4

我有一个 WinForms 应用程序,它以最简单的方式显示动画 gif - 有一个直接加载 .gif 的 PictureBox。

WinForms 设计器生成的代码如下所示:

        // 
        // pictureBoxHomer
        // 
        this.pictureBoxHomer.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
        this.pictureBoxHomer.Dock = System.Windows.Forms.DockStyle.Fill;
        this.pictureBoxHomer.Image = ((System.Drawing.Image)(resources.GetObject("pictureBoxHomer.Image")));
        this.pictureBoxHomer.Location = new System.Drawing.Point(3, 3);
        this.pictureBoxHomer.Name = "pictureBoxHomer";
        this.pictureBoxHomer.Size = new System.Drawing.Size(905, 321);
        this.pictureBoxHomer.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
        this.pictureBoxHomer.TabIndex = 0;
        this.pictureBoxHomer.TabStop = false;

图片当然是这样的:http: //media.tumblr.com/tumblr_m1di1xvwTe1qz97bf.gif

问题:虽然这个动画 gif 在浏览器中显示得非常好,但它在 WinForms 应用程序中运行得太快了,这并不像需要的那样快乐。所以:

问题:有没有办法在 WinForms 应用程序中减慢动画 gif 的速度?

4

2 回答 2

4

我相信答案与 C# 相比与图像相关。如果您在 GIMP 之类的工具中编辑该特定图像并查看图层,您会看到它由 10 个图层(帧)组成,但它们之间没有设置“延迟时间” - 它在图层中有 (0ms)属性。您可以编辑图层的属性并通过右键单击它并在菜单中选择该选项来更改它。当然,最后您必须导出新图像并将其保存为 GIF,在选项中选择“动画”。

我相信在这种情况下(当没有指定帧之间的延迟时间时)Web 浏览器和 C# PicutureBox 会强制使用它们自己不同的默认值。所以,如果你设置一个延迟,比如说 100 毫秒,就像步骤 3 中描述那样,你会让动画变慢。

于 2012-08-20T19:52:11.037 回答
0

为了将来参考,可以覆盖图片框中 GIF 的延迟时间。这是一个粗略的例子:

    public partial class Form1 : Form
{
    private FrameDimension dimension;
    private int frameCount;
    private int indexToPaint;
    private Timer timer = new Timer();

    public Form1()
    {
        InitializeComponent();

        dimension = new FrameDimension(this.pictureBox1.Image.FrameDimensionsList[0]);
        frameCount = this.pictureBox1.Image.GetFrameCount(dimension);
        this.pictureBox1.Paint += new PaintEventHandler(pictureBox1_Paint); 
        timer.Interval = 100;
        timer.Tick += new EventHandler(timer_Tick);
        timer.Start();
    }

    void timer_Tick(object sender, EventArgs e)
    {
        indexToPaint++;
        if(indexToPaint >= frameCount)
        {
            indexToPaint = 0;
        }
    }

    void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        this.pictureBox1.Image.SelectActiveFrame(dimension, indexToPaint);
        e.Graphics.DrawImage(this.pictureBox1.Image, Point.Empty);
    }
}
于 2012-08-20T20:53:38.317 回答