在 X 时间内在 C# 中加载动画 GIF 图像

maf*_*fap 3 c# animated-gif winforms

如何在我的表单中显示动画图像,控制其大小和持续时间?

我尝试过这样的事情:

private void AnimImage()
{
    PicBox.Enabled = true;
    PicBox.Visible = true;                
    System.Threading.Thread.Sleep(5000);
    PicBox.Visible = false;
    PicBox.Enabled = false;
}
Run Code Online (Sandbox Code Playgroud)

dev*_*avx 8

要在您的表单上显示动画图像,请执行以下操作;

1.)Drop a PictureBox on your Form.

2.)In the Properties Window of designer,change the image property so it contains the path to your image.

3.)Resize it as per your needs.

就是这样,现在尝试运行该项目,如果没有抛出异常,您将在PictureBox中看到您的图像动画。
在项目执行过程中的任何时候,如果您想更改图像,请使用以下声明;

pictureBox1.Load("Path to a new image");//Assuming you haven't renamed the PictureBox.
Run Code Online (Sandbox Code Playgroud)

此外,如果您想手动完成这些工作,请继续阅读;

private void DisplayImage()
{
    PictureBox pictureBox1=new PictureBox();
    pictureBox1.Location=new Point(Use appropriate values to place the control);
    this.Controls.Add(pictureBox1);
    pictureBox1.Load("Path to a image to display");
}
Run Code Online (Sandbox Code Playgroud)

当你不想显示PictureBox时,像其他用户说的那样将其visible属性设置为false,这样;

pictureBox1.Visible=false;
Run Code Online (Sandbox Code Playgroud)

并使用下面的代码取回它;

pictureBox1.Visible=true;
Run Code Online (Sandbox Code Playgroud)

更新 :

要仅显示图像 5 秒钟,请执行此操作;

Drop a Timer on your Form.

Set its Interval property to 5000 Milliseconds.

Create a new Event for its Tick Event (locate Tick event in Events Window and double click it).

Next modify DisplayImage() so it looks like :

private void DisplayImage()
{
    timer1.Start();
    PictureBox pictureBox1=new PictureBox();
    pictureBox1.Location=new Point(Use appropriate values to place the control);
    this.Controls.Add(pictureBox1);
    pictureBox1.Load("Path to a image to display");
}
Run Code Online (Sandbox Code Playgroud)

Next define an integer field(outside all functions) named count,like this;

private int count=0;
Run Code Online (Sandbox Code Playgroud)

Now modify timer1_Tick() event so it looks like below;

private void timer1_Tick(object sender, EventArgs e)
{
    count++;
    if (count == 5)
    {
        SourcePictureBox.Image = null;
        count = 0;
    }
}
Run Code Online (Sandbox Code Playgroud)

那应该可以完成工作。还有其他事情,请告诉我。