Srijani Ghosh
Srijani Ghosh

Reputation: 4216

Animation in .gif is not showing in windows Form

I am very new to c# and windows form.

What I am trying is to add a PictureBox to a windows form and display an animated .gif image before I display the data in the form.

Here is the code for the PictureBox :

private System.Windows.Forms.PictureBox pictureBox1;

            // 
            // pictureBox1
            // 
            this.pictureBox1.BackColor = System.Drawing.SystemColors.AppWorkspace;
            this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
            this.pictureBox1.Location = new System.Drawing.Point(374, 442);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(16, 16);
            this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
            this.pictureBox1.TabIndex = 19;
            this.pictureBox1.TabStop = false;
            this.pictureBox1.Visible = false;                

Now, on a button click the gif image should be loaded in the picture box . The code for that goes like this :

private void scanButton_Click(object sender, EventArgs e)
        {
            pictureBox1.Visible = true;
            pictureBox1.Refresh();  
        }

Now, when I click the button "Scan Button", the image becomes visible,but the animation is not working.

Please can anyone help me what is happening here ?

Thanks!

Upvotes: 3

Views: 3362

Answers (3)

Pieczarr
Pieczarr

Reputation: 358

If you really need to use picture box you can iterate over a collection of images. But i strongly recommend using WebBrowser (mind it is basically IE)

This should help you with all animations using windows forms Simple animation using C#/Windows Forms

Upvotes: -2

Kamalesh Wankhede
Kamalesh Wankhede

Reputation: 1495

For your .gif animation to work, your main (UI) thread should be free. If you are performing any process on main thread then you will not get animation effect.

From your question, it seems like you are performing Scanning operation on click of button. That may be the problem.

To get rid of this problem, you will have to make sure that your main thread is free and any operation (like Scanning) should be on separate thread.

Upvotes: 4

Arctic Vowel
Arctic Vowel

Reputation: 1522

Just assign the path to the image in ImageLocation.

pictureBox1.ImageLocation = "C:\\throbber.gif";

The PictureBox will understand that it's an animated image and play it. Disabling the PictureBox or the Form will stop the animation from playing.

Upvotes: 0

Related Questions