Reputation: 96
I need to restart a gif image so it will start from the first frame. I have little experience with gif images in C#. I work on a C# windows form application. My gif image is in a picturebox. I need to restart it each time a person for example presses a button.
I have tried doing:
pictureBox1.Image.SelectActiveFrame(new FrameDimension(pictureBox1.Image.FrameDimensionsList[0]), 0)
but it didnt show any change.
I also tried Image animator also no luck. I really need help it is for my final project in high school engineering.
Upvotes: 1
Views: 2288
Reputation: 63317
I found that Image.SelectActiveFrame()
doesn't seem to work because how it works is not what you thought. It just set the initial frame if there is some control reading its frame, the first frame read is the active frame. So after SelectActiveFrame()
you have to re-assign the Image
property of your pictureBox to that new Image, like this:
private void RestartToFrameIndex(int index){
pictureBox.Image.SelectActiveFrame(new FrameDimension(pictureBox.Image.FrameDimensionsList[0]), index);
pictureBox.Image = pictureBox.Image;
}
//If you want to restart to the first frame, just call the method above like this:
RestartToFrameIndex(0);
That's the general solution I've just found :) hope it helps others...
Upvotes: 4
Reputation: 1449
I tried this solution and It's working fine :
Image animated = pictureBox1.Image;
pictureBox1.Image = animated;
this way , you reset pictureBox1.Image
so the animated GIF starts from beginning.
Upvotes: 2