【问题标题】:Pause a GIF in a form暂停表格中的 GIF
【发布时间】:2013-07-06 21:33:17
【问题描述】:

只是想知道如何暂停 GIF 图像? 我正在访问服务器,我希望在发生某些事情并且系统冻结时播放 gif 图像,我希望它暂停。 我有一个附有图像的图片框。 这可能吗?

【问题讨论】:

  • 请标记为 winforms、WPF 或 ASP.NET
  • 既然您知道(看起来)状态何时发生变化(从空闲进行中,反之亦然),因此无论平台如何,一个可能的解决方案是拥有一个动画 gif 和一个正常且仅在适当的时候改变它们。

标签: c# picturebox gif


【解决方案1】:

PictureBox 使用 ImageAnimator 类为 GIF 图像设置动画。其中有 Stop() 方法来停止动画。不幸的是,它不会暴露你需要修改它的成员,你必须自己使用 ImageAnimator。

如果您不反对使用反射来绕过这些限制,那么您可以使用后门。这通常是一个相当糟糕的主意,但 Winforms 处于维护模式,并且 PictureBox 再次更改的可能性非常接近于零。它看起来像这样:

using System.Reflection;
...

    private static bool IsAnimating(PictureBox box) {
        var fi = box.GetType().GetField("currentlyAnimating",
            BindingFlags.NonPublic | BindingFlags.Instance);
        return (bool)fi.GetValue(box);
    }
    private static void Animate(PictureBox box, bool enable) {
        var anim = box.GetType().GetMethod("Animate", 
            BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(bool) }, null);
        anim.Invoke(box, new object[] { enable });
    }

此示例按钮的 Click 事件可靠地停止并启动了动画:

    private void button1_Click(object sender, EventArgs e) {
        Animate(pictureBox1, !IsAnimating(pictureBox1));
    }

如果您不喜欢这些技巧,请自己使用 ImageAnimator。

【讨论】:

  • 这会从第一帧重新开始播放动画。有什么方法可以真正暂停动画并从暂停的帧继续播放?
【解决方案2】:

将所需的帧转换为位图

        Image gifImg = ActivscreenLibraries.Resources.throbber_running;
        FrameDimension dimension = new FrameDimension(gifImg.FrameDimensionsList[0]);
        // Number of frames
        int frameCount = gifImg.GetFrameCount(dimension);
        // Return an Image at a certain index
        gifImg.SelectActiveFrame(dimension, 5);
        Bitmap aa = new Bitmap(gifImg);
        pictureBox1.Image = Image.FromHbitmap(aa.GetHbitmap());

【讨论】:

    【解决方案3】:

    您可以在 cmets 中阅读到 question Micha 已链接您可以简单设置

    myPictureBox.Enabled = false;
    

    停止图像动画。将其设置为true 以重新启动它。不需要反射。

    Windows.Forms.PictureBox.OnEnabledChanged 本质上是调用Animate(Enabled)。见source code

    【讨论】:

    • 这将停止动画,但是当您重新启用该框时,动画每次都会从头开始,这看起来很不和谐。
    猜你喜欢
    • 2012-03-30
    • 2016-03-14
    • 2016-10-18
    • 1970-01-01
    • 1970-01-01
    • 2012-01-21
    • 1970-01-01
    • 2015-06-22
    • 2018-04-08
    相关资源
    最近更新 更多