如何减慢GIF动画的速度

时间:2012-08-20 19:09:09

标签: c# winforms gif animated

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

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?

2 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:1)

为了将来参考,可以覆盖图片框中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);
    }
}