I have this class that im getting the information from the animated gif:
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections.Generic;
using System.IO;
public class AnimatedGif
{
private List<AnimatedGifFrame> mImages = new List<AnimatedGifFrame>();
public AnimatedGif(string path)
{
Image img = Image.FromFile(path);
int frames = img.GetFrameCount(FrameDimension.Time);
if (frames <= 1) throw new ArgumentException("Image not animated");
byte[] times = img.GetPropertyItem(0x5100).Value;
int frame = 0;
for (; ; )
{
int dur = BitConverter.ToInt32(times, 4 * frame);
mImages.Add(new AnimatedGifFrame(new Bitmap(img), dur));
if (++frame >= frames) break;
img.SelectActiveFrame(FrameDimension.Time, frame);
}
img.Dispose();
}
public List<AnimatedGifFrame> Images { get { return mImages; } }
}
public class AnimatedGifFrame
{
private int mDuration;
private Image mImage;
internal AnimatedGifFrame(Image img, int duration)
{
mImage = img; mDuration = duration;
}
public Image Image { get { return mImage; } }
public int Duration { get { return mDuration; } }
}
Then in Form1 in the constructor im looping over the List of images and get the duration of each of the images. So in the List in this case there are 4 images each one duration is 1.
So when im showing the animatd gif im trying to display the speed in a label.
But there are two problems:
- The loop in the end show me that speed is now 2 not 4.
- The label the result there is all the time 0 and 2/100 is not 0.
What i want to do is to show the actual speed.
Like in the program Easy gif animator 5:
If im standing on one image from the 4 i see that its speed is 0.01seconds wich is Delay 1 wich is 1/100th sec.
And if i mark all the images in the program i get speed 0.04 seconds.
Maybe i confusing between speed and duration.
I want to get the speed of the animated gif.
This is my code in Form1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MyAnimatedGifEditor
{
public partial class Form1 : Form
{
int speed;
Image myImage;
AnimatedGif myGif;
public Form1()
{
InitializeComponent();
myImage = Image.FromFile(@"D:\fananimation.gif");
myGif = new AnimatedGif(@"D:\fananimation.gif");
for (int i = 0; i < myGif.Images.Count; i++)
{
speed = myGif.Images[i].Duration;
speed++;
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
pictureBox1.Image = myImage;
label2.Text = (speed / 100).ToString();
}
}
}
Image from the easy gif animator after selected all images:

In the end i want to show on two labels one the duration of the animated gif and one the speed.
There is a problem with the portion of your code where you are looping through the images of the animated gif. You probably want two separate variables here instead of just
speed.So something like
And to solve your integer math problem, divide by 100.0 instead of 100: