【问题标题】:Load animated GIF image in C# during X time在 X 时间内在 C# 中加载动画 GIF 图像
【发布时间】:2013-08-31 13:25:31
【问题描述】:

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

我尝试过这样的事情:

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

【问题讨论】:

  • 不知道是否有更好的方法,但您可以使用模态对话框和更新面板来做到这一点。或者只是使用 jquery 和 ajax 调用...多种口味可以满足您的需求
  • 只使用一个图片框。如果您不想再查看它,请将其 Visible 属性设置为 false。
  • 对于动画图像,您可以使用此控制器。 codeproject.com/Tips/1004624/Gif-viewer-Snipper-control

标签: c# winforms animated-gif


【解决方案1】:

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

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.

另外,如果您想手工完成,请继续阅读;

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 秒,请执行此操作;

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

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

private int count=0;

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

这应该可以完成工作。还有什么,请告诉我。

【讨论】:

  • 这没关系,但我无法控制图像显示的时间
  • @mafap ,control the time the image is displayed 是什么意思?
  • 例如显示图片5秒
  • @mafap 当你需要从PictureBox中移除图片时,设置它的image属性为null,像这样SourceImageBox.Image=null。另外如果你想停止引导线,包围MouseMove_Event中的代码和 OnPaint_Event 带有 `if-else 语句,当你想隐藏它们时提供 false 并让它们返回提供 true。如果你需要代码,请通知我,我会尽力为你获取它。
  • 看起来不错的解决方案,我想在鼠标移动时将图像属性设置为 false。你能给我一些代码吗?
【解决方案2】:

在表单中添加图片框并在图片框中添加 .gif 图像。在加载表单时使图片框可见性为 true。

请确保在加载时已启用图片框,否则windows窗体中将不会出现任何图像动画。

【讨论】:

    【解决方案3】:

    打开您的表单 在您的解决方案资源管理器中打开您的项目名称,打开属性并右键单击要添加到它们的资源。 拖动您的 .Gif 图像并将其设置为启用并保存。

    添加一个图片框并将图像设置为您的 Gif(不是背景图像,否则它将不起作用)不要停靠,否则它会重置计时器,因此 GIF 将循环播放。

    添加一个计时器,将其设置为 5000(5 秒)并启用它。

    双击你的图片框并输入

    pictureBox1.Visible = false;  (assuming you have not renamed your picturebox) (NOTE this is a back up encase the timer fails)
    

    返回您的表单并双击您的计时器,然后添加 图片框1.可见=假; timer1.Stop(); (防止定时器在加载时启动)

    一旦时间到了,您的图片现在应该关闭(如果没有,请点击它)

    如果您需要您的 Gif 在单击按钮时开始 在已初始化写入其中的私有 void 中 添加图片框1.Visible = false;

    现在添加您的按钮并将以下代码添加到 button1_click

    pictureBox1.Visible = true; Timer1.Start(); (这是用于在按钮单击时启动计时器的代码) 在此输入代码

    如果这会导致循环错误,即 gif 没有隐藏,只需输入 timer.Stop();在picturebox_click中。

    【讨论】:

      猜你喜欢
      • 2013-08-04
      • 2016-05-24
      • 2013-08-09
      • 1970-01-01
      • 2015-09-07
      • 2011-05-22
      • 1970-01-01
      • 2018-04-22
      • 2010-12-05
      相关资源
      最近更新 更多