How to show a specific frame of a GIF image in C#?

跟風遠走 提交于 2019-12-01 06:06:44

问题


I want to show a frame of a gif image. I searched and found that the following code should work, but it doesn't work. it detects the number of frames correctly but it shows the whole frames of gif instead of the specified frame. thanks everybody.

Image[] frames = new Image[36];
Image GG = Image.FromFile(@"C:\Users\Administrator\TEST C#\TEST2frame2\chef.gif");
FrameDimension dimension = new FrameDimension(GG.FrameDimensionsList[0]);
            // Number of frames
int frameCount = GG.GetFrameCount(dimension);
label1.Text = frameCount.ToString();

            // Return an Image at a certain index
GG.SelectActiveFrame(dimension, 1);
frames[1] = ((Image)GG.Clone());
pictureBox1.Image = frames[1];

回答1:


Use your own code up until the call of SelectActiveFrame() and after that change to this lines:

frames[0] = new Bitmap(GG);
pictureBox1.Image = frame[0];

This should do the trick. Please do not forget do dispose the created Images.




回答2:


Oh it works, but not as you expect it to.

When you set an active frame of a gif image it actually restarts its animation from that frame. You have to stop it when you change a frame, by setting the pictureBox.IsEnabled to false, for instance. Try the following code

private Image img;

public Form1()
{
    InitializeComponent();
    img = Image.FromFile(@"C:\Users\Administrator\TEST C#\TEST2frame2\chef.gif");
    pictureBox1.Image = img;
}

private void button1_Click(object sender, EventArgs e)
{
    var dim = new FrameDimension(img.FrameDimensionsList[0]);
    img.SelectActiveFrame(dim, 1);
    pictureBox1.Enabled = false;
}

Try pressing the button in different moments and you will see that the active image frame will change.



来源:https://stackoverflow.com/questions/27874353/how-to-show-a-specific-frame-of-a-gif-image-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!