c# - 在 X 时间内在 C# 中加载动画 GIF 图片

标签 c# winforms animated-gif

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

我尝试过这样的事情:

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

最佳答案

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

1.)在您的表单上放置一个 PictureBox。

2.)在设计器的属性窗口中,更改图像属性,使其包含图像的路径。

3.)根据您的需要调整它的大小。

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

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

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

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");
}

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

pictureBox1.Visible=false;

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

pictureBox1.Visible=true;

更新:

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

在您的表单上放置一个计时器。

将其 Interval 属性设置为 5000 毫秒。

为其 Tick 事件创建一个新事件(在事件窗口中找到 Tick 事件并双击它)。

接下来修改 DisplayImage() 使其看起来像:

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");
}

接下来定义一个整数字段(在所有函数之外),命名为count,像这样;

private int count=0;

现在修改 timer1_Tick() 事件,如下所示;

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

这应该可以完成工作。还有其他问题,请告诉我。

关于c# - 在 X 时间内在 C# 中加载动画 GIF 图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18548655/

相关文章:

c# 将 .csv 文件读入具有重复列名的 DataTable

c# - 使用C#将文本框中的数据写入ASP.net中的文本文件

c# - 如何将 ContextMenuStrip 添加到 ToolStripMenuItem

javascript - 隐藏或删除 html 加载器?

c++ - Qt显示透明动画gif

c# - Linq 和 var 关键字

c# - 什么时候不使用 yield (return)

c# - MSChart 没有填满整个图表区域

c# - 如何在应用程序内部使用基本身份验证调用 url

javascript - 如何让我的按钮调用 GIF 旋转?