How do you show animated GIFs on a Windows Form (c#)
Note that in Windows, you traditionally don't use animated Gifs, but little AVI animations: there is a Windows native control just to display them. There are even tools to convert animated Gifs to AVI (and vice-versa).
I had the same problem. Whole form (including gif) stopping to redraw itself because of long operation working in the background. Here is how i solved this.
private void MyThreadRoutine()
{
this.Invoke(this.ShowProgressGifDelegate);
//your long running process
System.Threading.Thread.Sleep(5000);
this.Invoke(this.HideProgressGifDelegate);
}
private void button1_Click(object sender, EventArgs e)
{
ThreadStart myThreadStart = new ThreadStart(MyThreadRoutine);
Thread myThread = new Thread(myThreadStart);
myThread.Start();
}
I simply created another thread to be responsible for this operation. Thanks to this initial form continues redrawing without problems (including my gif working). ShowProgressGifDelegate and HideProgressGifDelegate are delegates in form that set visible property of pictureBox with gif to true/false.
It's not too hard.
- Drop a picturebox onto your form.
- Add the .gif file as the image in the picturebox
- Show the picturebox when you are loading.
Things to take into consideration:
- Disabling the picturebox will prevent the gif from being animated.
Another way of doing it:
Another way that I have found that works quite well is the async dialog control that I found on the code project
If you put it in a PictureBox control, it should just work