155

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?

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
Stuart Helwig
  • 9,318
  • 8
  • 51
  • 67
  • for animated an image you can use this controller. http://www.codeproject.com/Tips/1004624/Gif-viewer-Snipper-control – xwpedram Jun 27 '15 at 21:18

9 Answers9

271

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

FryHard
  • 10,305
  • 7
  • 35
  • 38
  • 8
    Embarrassingly easy! MY initial "Googling" didn't show this - maybe it is too obvious. Thanks. – Stuart Helwig Oct 03 '08 at 05:13
  • 2
    The picturebox will not refresh (redraw) as the process executing, however. – LarryBud Jan 30 '15 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 '15 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 06 '16 at 20:57
  • @LarryBud To cause it to refresh (redraw) as the process is executing, you can call `Application.DoEvents();`. Possible approach: your business logic fires an event when something is partially done, then whatever properties (for instance progress bar or percentage) can be updated and animation can be updated with Application.DoEvents() – Do-do-new Sep 04 '17 at 09:02
  • Its funny how, *as of this writing*, the ajax load website is still up and functional. Web 2.0 it says...what version of web are we on now? ;-) I've been using it for 10 years or so...great site. – ΩmegaMan Jul 09 '20 at 21:29
  • 1
    The AjaxLoad website is no longer functional – slayernoah Mar 23 '22 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.

Aruch
  • 121
  • 1
  • 2
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).

PhiLho
  • 40,535
  • 6
  • 96
  • 134
  • Yes - perhaps that's what I should've asked. Thanks. – Stuart Helwig Oct 03 '08 at 05:24
  • 7
    Link to tool, s'il vous plaît? Or any other reference to documentation, reasons why, etc., would be great. Thanks! – Jeff B Aug 02 '13 at 21:58
  • 1
    I do not care about writing traditional Windows software. If it is supported by the `System.Windows.Forms.PictureBox` control, i'll just use animated GIFs. – Mike de Klerk Sep 16 '13 at 12:03
  • What is the windows native control to display avis?? – Jay Croghan Aug 19 '22 at 11:50
  • I'm not positive, but I *think* one of these may be what they were referring-to: https://learn.microsoft.com/en-us/windows/win32/controls/animation-control-overview OR https://www.codeproject.com/Articles/159/CAnimateCtrl-Example – NetXpert Oct 22 '22 at 06:46
3

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

Grank
  • 5,242
  • 7
  • 33
  • 36
1

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

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
Gehan Fernando
  • 1,221
  • 13
  • 24
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;

Ameer Adel
  • 82
  • 1
  • 1
  • 12
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;
}
M. Fawad Surosh
  • 450
  • 7
  • 20
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.

Byron
  • 16
  • 8