How do you show animated GIFs on a Windows Form (c#)
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
C# PictureBox with animated GIF skips Frames
Use this code. Since 25 frames per second is displayed, I set the timer to 40, which means one frame every 40 milliseconds. (1000ms / 25 frames = 40ms)
step 1. This method shows how to use
static Image[] images;
int frameCount = 0;
private void Btn_Click(object sender, EventArgs e)
{
//get gif image
object ezgif_com_video_to_gif = Resources.ResourceManager.GetObject("ezgif_com_video_to_gif");
images = getFrames((Image)ezgif_com_video_to_gif);//convert to frames array
//show frames
System.Timers.Timer timer = new System.Timers.Timer();
timer.Interval = 40;
timer.Elapsed += Timer_Elapsed;
timer.Start();
}
step 2. add timer tick
private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
pictureBox1.Image = images[frameCount];
frameCount++;
if (frameCount > images.Length - 1)
frameCount = 0;
}
step 3. convert gif to frames
Image[] getFrames(Image originalImg)
{
int numberOfFrames = originalImg.GetFrameCount(FrameDimension.Time);
Image[] frames = new Image[numberOfFrames];
for (int i = 0; i < numberOfFrames; i++)
{
originalImg.SelectActiveFrame(FrameDimension.Time, i);
frames[i] = ((Image)originalImg.Clone());
}
return frames;
}
Showing animated gif in Winforms without locking the file
Using a MemoryStream is indeed the right way to avoid the file lock. Which is a strong optimization btw, the lock is created by the memory-mapped file that the Image class uses to keep the pixel data out of the paging file. That matters a great deal when the bitmap is large. Hopefully not on an animated gif :)
A small mistake in your code snippet, you forgot to reset the stream back to the start of the data. Fix:
using (var fs = new System.IO.FileStream(...)) {
var ms = new System.IO.MemoryStream();
fs.CopyTo(ms);
ms.Position = 0; // <=== here
if (pic.Image != null) pic.Image.Dispose();
pic.Image = Image.FromStream(ms);
}
In case it needs to be said: do not dispose the memory stream. That causes very hard to diagnose random crashes later, pixel data is read lazily.
Winform applicaton not animating picturebox gif while processing data from Excel
In case you have a loop inside of youre code add
Application.DoEvents()
inside of the loop
Related Topics
How to Retain Callsite Information When Wrapping Nlog
JSON.Net Custom Serialization with JSONconverter - How to Get the "Default" Behavior
Why Is Foreach Loop Read-Only in C#
Running Async Methods in Parallel
Dynamically Changing Schema in Entity Framework Core
How to Get All Cookies of a Cookiecontainer
How to Programmatically Convert Vb6 Formatting Strings to .Net Formatting Strings
Ssis Task for Inconsistent Column Count Import
Can Someone Explain How Bcrypt Verifies a Hash
Openssl Encryption Using .Net Classes
Encoding Xpath Expressions with Both Single and Double Quotes
How to Enable Nullable Reference Types Feature of C# 8.0 for the Whole Project
Threadpool Not Starting New Thread Instantly
Client-Side Validation in Custom Validation Attribute - ASP.NET MVC 4
Cross-Thread Winforms Control Editing
Get Screen Resolution in Win10 Uwp App