1

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

4

3 回答 3

5

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。

于 2013-07-09T10:48:51.727 回答
1

将所需的帧转换为位图

        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());
于 2013-10-02T13:18:49.200 回答
1

正如您可以在对Micha 链接的问题的评论中阅读的那样,您可以简单地设置

myPictureBox.Enabled = false;

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

Windows.Forms.PictureBox.OnEnabledChanged本质上调用Animate(Enabled). 请参阅源代码

于 2016-05-25T11:38:26.910 回答