156

I have a form showing progress messages as a fairly long process runs. It's a call to a web service so I can't really show a percentage complete figure on a progress bar meaningfully. (I don't particularly like the Marquee property of the progress bar)

I would like to show an animated GIF to give the process the feel of some activity (e.g. files flying from one computer to another like Windows copy process).

How do you do this?

1

9 Answers 9

272

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

7
  • 8
    Embarrassingly easy! MY initial "Googling" didn't show this - maybe it is too obvious. Thanks. Oct 3, 2008 at 5:13
  • 2
    The picturebox will not refresh (redraw) as the process executing, however.
    – LarryBud
    Jan 30, 2015 at 20:45
  • Durr, I had set it as the background image by mistake. Background image, reasonably, doesn't support animation; foreground image does.
    – neminem
    Dec 23, 2015 at 16:47
  • @neminem: Same here! That's why I'm here. For a while I thought pictureBox didn't support fox gifs (first time ever I'm using it)
    – Jack
    May 6, 2016 at 20:57
  • 1
    The AjaxLoad website is no longer functional
    – slayernoah
    Mar 23, 2022 at 13:06
12

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.

7

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).

5
3

If you put it in a PictureBox control, it should just work

1
  • 2
    Unless you disable the form ;)
    – Jeff B
    Aug 2, 2013 at 21:56
1

It doesn't when you start a long operation behind, because everything STOPS since you'Re in the same thread.

1
  • And MTA thread mode seems to be the worst solution as far as I am reading on StackOverflow.
    – Yogurtu
    Jan 13, 2016 at 15:46
1
Public Class Form1

    Private animatedimage As New Bitmap("C:\MyData\Search.gif")
    Private currentlyanimating As Boolean = False

    Private Sub OnFrameChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Me.Invalidate()

    End Sub

    Private Sub AnimateImage()

        If currentlyanimating = True Then
            ImageAnimator.Animate(animatedimage, AddressOf Me.OnFrameChanged)
            currentlyanimating = False
        End If

    End Sub

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)

        AnimateImage()
        ImageAnimator.UpdateFrames(animatedimage)
        e.Graphics.DrawImage(animatedimage, New Point((Me.Width / 4) + 40, (Me.Height / 4) + 40))

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        BtnStop.Enabled = False

    End Sub

    Private Sub BtnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStop.Click

        currentlyanimating = False
        ImageAnimator.StopAnimate(animatedimage, AddressOf Me.OnFrameChanged)
        BtnStart.Enabled = True
        BtnStop.Enabled = False

    End Sub

    Private Sub BtnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStart.Click

        currentlyanimating = True
        AnimateImage()
        BtnStart.Enabled = False
        BtnStop.Enabled = True

    End Sub

End Class
1
  • This is not C#, the question specified the language as C# not VB
    – Dev-Siri
    Dec 25, 2022 at 18:33
1

too late, But! setting the image to the PictureBox.Image and Setting the PictureBox.SizeMode = PictureBoxSizeMode.Zoom does the trick .Net Framework 4.8

PictureBox.Image = Image.FromFile("location"); // OR From base64 PictureBox.SizeMode = PictureBoxSizeMode.Zoom;

0

I had the same issue and came across different solutions by implementing which I used to face several different issues. Finally, below is what I put some pieces from different posts together which worked for me as expected.

private void btnCompare_Click(object sender, EventArgs e)
{
    ThreadStart threadStart = new ThreadStart(Execution);
    Thread thread = new Thread(threadStart);
    thread.SetApartmentState(ApartmentState.STA);
    thread.Start();
}

Here is the Execution method that also carries invoking the PictureBox control:

private void Execution()
{
    btnCompare.Invoke((MethodInvoker)delegate { pictureBox1.Visible = true; });
    Application.DoEvents();

    // Your main code comes here . . .

    btnCompare.Invoke((MethodInvoker)delegate { pictureBox1.Visible = false; });
}

Keep in mind, the PictureBox is invisible from Properties Window or do below:

private void ComparerForm_Load(object sender, EventArgs e)
{
    pictureBox1.Visible = false;
}
0

Pictureboxes in WinForms support multiple different file extensions. Alongside having support for PNG files it can also support GIFs. So really, all you have to do is set the image in the picturebox to a GIF. Please note that disabling the Picturebox causes it to stay as a static picture.

You can modify the image through code or by using the properties tab of the Picturebox.

Code

pictureBox1.Image = ImageFileHere;

If that doesn't work, you can also try:

pictureBox1.Image = Image.FromFile("ImageFileHere");

Properties

Just look for the 'Image' property. NOT BackgroundImage.


Don't forget to change the size mode in case it doesn't format properly! You can do that in both ways too.

Code

pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; (There are a few different modes to choose from, this one is Stretch)

Properties

Look for the 'SizeMode' property.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.