How to Show Animated Gifs on a Windows Form (C#)

How do you show animated GIFs on a Windows Form (c#)

It's not too hard.

  1. Drop a picturebox onto your form.
  2. Add the .gif file as the image in the picturebox
  3. 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

Animated GIF in Windows Form while executing long process

It is not that hard to add a BackgroundWorker

  • Open your form in the designer
  • Open the Toolbox (ctrl+alt+X)
  • Open the category Components
  • Drag the Backgroundworker on your From

You will end-up with something like this:

winform with backgroundworker

You can now switch to the event view on the Properties tab and add the events for DoWork and RunWorkerCompleted

event properties

The following code goes in these events, notice how DoWork use the DowWorkEventArgs Argument property to retrieve the value that is supplied in RunWorkerAsync.

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
// start doing what ever needs to be done
// get the argument from the EventArgs
string comboboxValue = (string) e.Argument; // if Argument isn't string, this breaks
// remember that this is NOT on the UI thread

// do a lot of work here that takes forever
System.Threading.Thread.Sleep(10000);
// afer this the completed event is fired
}

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// this runs on the UI thread
Loading_Off();
}

Now you only need to start the background job, for example from a button click event to call RunWorkerAsync

    private void button1_Click(object sender, EventArgs e)
{
Loading_On();
backgroundWorker1.RunWorkerAsync(comboBox1.SelectedItem); // pass a string here
}

Done! You have successfully added a backgroundworker to your form.

GIF Animation not working in Windows Form


Solved

My current Thread is busy to Play GIF Animation.

I tried So many ways like Application.DoEvents(); etc.
But every thing can't helped me.

The answer in the following Question, which uses Threading is a very great working idea.

StackOverFlow: Show/Hide Splash Screen

Thanks for every one.

Load animated GIF image in C# during X time

To display an animated image on your Form,do the following;

1.)Drop a PictureBox on your Form.

2.)In the Properties Window of designer,change the image property so it contains the path to your image.

3.)Resize it as per your needs.

That's it,now try to run the project,if no exception is thrown you will see your image animating in the PictureBox.

Anytime during the course of execution of the project if you want to change the image,use the statement below;

pictureBox1.Load("Path to a new image");//Assuming you haven't renamed the PictureBox.

Additionally,if you would like to do the stuff by hand,read along;

private void DisplayImage()
{
PictureBox pictureBox1=new PictureBox();
pictureBox1.Location=new Point(Use appropriate values to place the control);
this.Controls.Add(pictureBox1);
pictureBox1.Load("Path to a image to display");
}

When you don't want the PictureBox to be shown,set its visible property to false just like other users said,in this way;

pictureBox1.Visible=false;

and to get it back use the code below;

pictureBox1.Visible=true;

Update :

To display the image only for 5 seconds do this;

Drop a Timer on your Form.

Set its Interval property to 5000 Milliseconds.

Create a new Event for its Tick Event (locate Tick event in Events Window and double click it).

Next modify DisplayImage() so it looks like :

private void DisplayImage()
{
timer1.Start();
PictureBox pictureBox1=new PictureBox();
pictureBox1.Location=new Point(Use appropriate values to place the control);
this.Controls.Add(pictureBox1);
pictureBox1.Load("Path to a image to display");
}

Next define an integer field(outside all functions) named count,like this;

private int count=0;

Now modify timer1_Tick() event so it looks like below;

private void timer1_Tick(object sender, EventArgs e)
{
count++;
if (count == 5)
{
SourcePictureBox.Image = null;
count = 0;
}
}

That should do the job.Anything else,please let me know.

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.



Related Topics



Leave a reply



Submit