GIF Animation not working in Windows Form
Asked Answered
E

7

5

I have 2 WinForms

Form2 uses Infragistics ultraTabControl. On Tab Changing im Showing Form1.

In Form1

I have a PictureBox assigned with animated GIF

In Form2

I am displaying Form1 like this.

Form1 frmOne=new Form1();
frmOne.Show();

Problem

GIF is not playing animation.

Epigynous answered 28/3, 2012 at 10:20 Comment(6)
you are constructing your froOne incorrectly. It should be "new Form1()" instead on "new Form()". Compiler should give you an error.Wendall
@ToKa sorry.it is "new Form1()" only.Epigynous
share some code for your prcoess, if still having some problem.Ataxia
A gif will not animate when the UI thread is busy with something else. Your code snippet gives no clue what it might be busy with. Post code that reproduces the problem. Use BackgroundWorker to offload busy work to another thread.Loriloria
@Epigynous If the behavior is temporary I would recommend doing performance profiling to see what is preventing the UI from updating when showing your form. If you have Visual Studio 2010 Ultimate you can do this with the profiling tools: msdn.microsoft.com/en-us/magazine/cc337887.aspx Does the animation start after a delay or does it not show because it is removed after the tab switches?Flashy
for animated an image you can use this controller. codeproject.com/Tips/1004624/Gif-viewer-Snipper-controlProbable
E
1

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.

Epigynous answered 29/3, 2012 at 5:48 Comment(0)
P
9

Make sure you have your image in the image property NOT THE BACKGROUND IMAGE PROPERTY. That's the mistake I made

Plumbo answered 10/1, 2014 at 2:31 Comment(0)
A
4

It is working fine for me. I have just dragged a new pictureBox on form and setting image property at Form_Load() event and showing GIF animated.

I did same as you on button click:

  TestForms.NavBarTest newForm = new TestForms.NavBarTest();
            newForm.Show();

At my Test Form:

private void NavBarTest_Load(object sender, EventArgs e)
        {
            pictureBox1.Image = NavronChartControl.Properties.Resources.url;
        }

Note: If your picture box is disabled then it will not animate the Gif

Reference:
How do you show animated GIFs on a Windows Form (c#)
Animated Progress Indicator in C# (Windows Forms)

Try to implement using this link:Animated GIF in picturebox won't animate apprach.

Ataxia answered 28/3, 2012 at 10:50 Comment(2)
thanks for ur answer. I tried out a sample. Its working. But its not working in My App. Iam just edited the question. Please refer that.Epigynous
My form was disabled. Your comment helped me out. Thanks.Geosyncline
S
3

The best way to achieve it is to run the animation in an async task, but accordingly, some limitations are possible to do that on windows form using:

System.Threading.Thread.Sleep(int milliseconds).

My splash view is displayed with a gif (loading)

e.g.: In your constructor,

public partial class MainMenu : Form
{

    private SplashScreen splash = new SplashScreen();

    public MainMenu ()
    {
        InitializeComponent();

        Task.Factory.StartNew(() => {
            splash.ShowDialog();
        });

       Thread.Sleep(2000);
   }

It is imperative to put the Thread.Sleep(int) after starting a new one, don't forget that every action you did on this thread needs to be invoked, for example:

    void CloseSplash()
    {
        Invoke(new MethodInvoker(() =>
        {
           splash.Close();
        }));
    }

Now your gif should work!

Stately answered 12/8, 2015 at 18:14 Comment(0)
E
1

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.

Epigynous answered 29/3, 2012 at 5:48 Comment(0)
O
0

This question has already raised before. Though the approach is different but the questions concept is the same

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

Source project where you can use Code Project

I guess that will help you out

Osorio answered 28/3, 2012 at 12:23 Comment(0)
A
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;
}
Alfredoalfresco answered 8/2, 2017 at 1:3 Comment(0)
R
-2

If none of the previous answers work for you, another idea is to refresh the image tag or write it on the fly with jQuery.
In my case I needed to show an animated GIF when a button on the form was clicked. Previously it would show up but not animate. I just added a jQuery event when the button was clicked that added the image tag on-the-fly to a div through the parent tag's html() function.

$('.showhidegif').html("<img src='/Content/img/angled-spinner.gif' />");

<div class="col-sm-12 showhidegif"></div>

Worth a shot especially if you're already using jquery on your page.

Rawalpindi answered 31/1, 2017 at 20:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.