C# GIF 动画在 Windows 窗体中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9905493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 11:09:00  来源:igfitidea点击:

GIF Animation not working in Windows Form

c#winformsinfragisticspicturebox

提问by aWebdesigner09

I have 2 WinForms

我有 2 个 WinForm

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

Form2 使用 Infragistics ultraTabControl。在选项卡上更改即时显示 Form1。

In Form1

在 Form1 中

I have a PictureBoxassigned with animated GIF

我有一个PictureBox带有动画的分配GIF

In Form2

在 Form2 中

I am displaying Form1 like this.

我正在像这样显示 Form1。

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

Problem

问题

GIFis not playing animation.

GIF不播放动画。

采纳答案by aWebdesigner09

Solved

解决了

My current Thread is busy to Play GIF Animation.

我当前的线程正忙于播放 GIF 动画。

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

我尝试了很多方法,比如 Application.DoEvents(); 等等,但每件事都帮不了我。

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

以下问题中使用线程的答案是一个非常好的工作理念。

StackOverFlow: Show/Hide Splash Screen

StackOverFlow:显示/隐藏启动画面

Thanks for every one.

感谢每一位。

回答by Niranjan Singh

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.

它对我来说很好。我刚刚在表单上拖动了一个新的图片框,并在 Form_Load() 事件中设置了图像属性并显示了 GIF 动画。

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

注意:如果您的图片框被禁用,则它不会为 Gif 设置动画

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

参考:
如何在 Windows 窗体上显示动画 GIF (c#)
C# 中的动画进度指示器(Windows 窗体)

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

尝试使用此链接实现:图片框中的动画 GIF 不会为apprach设置动画

回答by MDMalik

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

StackOverFlow:如何在 Windows 窗体上显示动画 GIF (c#)

Source project where you can use Code Project

您可以在其中使用代码项目的源项目

I guess that will help you out

我想这会帮助你

回答by Help

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

确保您的图像位于图像属性中,而不是背景图像属性中。那是我犯的错误

回答by erasmosoares

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:

实现它的最佳方法是在异步任务中运行动画,但相应地,在 Windows 窗体上使用以下方法可能存在一些限制:

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

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

My splash view is displayed with a gif (loading)

我的初始视图显示为 gif(加载中)

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:

必须Thread.Sleep(int)在开始一个新的之后放置,不要忘记你在这个线程上所做的每一个动作都需要被调用,例如:

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

Now your gif should work!

现在你的 gif 应该可以工作了!

回答by singleTrackVale

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.

如果之前的答案都不适合您,另一个想法是刷新图像标签或使用 jQuery 即时编写它。
就我而言,我需要在单击表单上的按钮时显示动画 GIF。以前它会显示但不会动画。我只是在单击按钮时添加了一个 jQuery 事件,该事件通过父标记的 html() 函数将图像标记即时添加到 div。

$('.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.

值得一试,特别是如果您已经在页面上使用 jquery。

回答by M. Fawad Surosh

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 Executionmethod that also carries invoking the PictureBox control:

下面是Execution方法,它也带有调用 PictureBox 控件:

    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 Windowor do below:

请记住,PictureBox 在属性窗口中是不可见的,或者执行以下操作:

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