How to slow down gif animation

I have a WinForms application that displays an animated gif in the easiest way - there is a PictureBox that directly loads .gif.

The code generated by the WinForms constructor is as follows:

// // 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; 

The image, of course, looks like this: http://media.tumblr.com/tumblr_m1di1xvwTe1qz97bf.gif

Problem: although this animated gif appears unexpectedly in the browser, it works too fast in the WinForms application, which is not as good as it needs to be. So:

Question: is there a way to slow down an animated gif in a WinForms application?

+4
source share
2 answers

I believe the answer is more about the image than about C #. If you edit this image in a tool such as GIMP and look at the layers, you will see its composition of 10 layers (frames), but there is no "delay time" set between them - it has an (0ms) attribute in the layer. You can edit the attribute of a layer and change it by right-clicking on it and selecting this option in the menu. Of course, in the end you should export your new image and save it as a GIF by selecting the "animated" options.

I believe in this case (when the delay time between frames is not indicated) the web browser and C # PicutureBox force their own, different default values. So, if you put a delay, say 100 ms, as described here in step 3, you will reduce the animation.

+4
source

For future reference, you can override the GIF delay time in the image window. Here is an example:

  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); } } 
+1
source

All Articles