简体   繁体   English

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

[英]Load animated GIF image in C# during X time

How can I show an animated image in my Form, controlling its size and time duration? 如何在表单中显示动画图像 ,并控制其大小和持续时间?

I tryed something like this: 我尝试过这样的事情:

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

To display an animated image on your Form,do the following; 要在窗体上显示动画图像,请执行以下操作;

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.

That's it,now try to run the project,if no exception is thrown you will see your image animating in the PictureBox. 就是这样,现在尝试运行该项目,如果没有异常抛出,您将在PictureBox中看到动画的图像。
Anytime during the course of execution of the project if you want to change the image,use the statement below; 在项目执行过程中的任何时候,如果要更改图像,请使用以下语句;

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

Additionally,if you would like to do the stuff by hand,read along; 另外,如果您想手工做这些东西,请继续阅读;

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

When you don't want the PictureBox to be shown,set its visible property to false just like other users said,in this way; 当您不希望显示PictureBox时,就像其他用户所说的那样,将其visible属性设置为false;

pictureBox1.Visible=false;

and to get it back use the code below; 并使用下面的代码找回它;

pictureBox1.Visible=true;

Update : 更新:

To display the image only for 5 seconds do this; 要仅显示图像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;
    }
}

That should do the job.Anything else,please let me know. 那应该做的。其他任何事情,请告诉我。

Add a picturebox to your form and Add the .gif image in the picturebox.Make the picture box visibility to true while loading the form. 在您的表单中添加一个图片框,并在图片框中添加.gif图像。在加载表单时,使图片框的可见性为true。

Please make sure that the picturebox is enabled at the time of loading otherwise there wont be any animation of the image in the windows form. 请确保在加载时启用了图片框,否则Windows窗体中的图像不会有任何动画。

open your form in your solution explorer open your project name, open properties and right click the resources to add to them. 在解决方案资源管理器中打开表单,打开项目名称,打开属性,然后右键单击资源以添加到其中。 drag your .Gif image and set it to enabled and save. 拖动.Gif图片并将其设置为启用并保存。

add a pictureBox and set the image as your Gif (NOT BACK GROUND IMAGE OR IT WILL NOT WORK) DO NOT DOCK OR IT RESETS THE TIMER SO GIF WILL LOOP. 添加一个pictureBox并将图像设置为您的Gif(不要返回地面图像,否则它将不起作用)。请勿停放它或重置计时器,这样GIF就会循环播放。

add a timer, set it to 5000 (5 seconds) and Enable it. 添加一个计时器,将其设置为5000(5秒)并启用它。

double click your picture box and enter 双击您的图片框并输入

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

go back to your form and double click your timer, and add pictureBox1.Visible = false; 返回您的表单并双击您的计时器,并添加pictureBox1.Visible = false;。 timer1.Stop(); timer1.Stop(); (prevent the timer starting on load up) (防止计时器在加载时启动)

your image should now close once time is up (just click it if not) 您的图片现在应该在时间到时关闭(如果没有,请单击它)

if you need your Gif to start on a button click in private void that has initialize written in it add pictureBox1.Visible = false; 如果您需要Gif从按钮开始,请在已写入初始化的私有void中单击添加pictureBox1.Visible = false;。

now add your button and code the following to button1_click 现在添加您的按钮并将以下代码编码为button1_click

pictureBox1.Visible = true; pictureBox1.Visible = true; Timer1.Start(); Timer1.Start(); (this is the code used to start the timer upon button click) enter code here (这是用于在单击按钮时启动计时器的代码)在此处输入代码

IF THIS CAUSES A LOOPING ERROR ie gif does not hide, simply put timer.Stop(); 如果这引起循环错误,即gif没有隐藏,则只需放置timer.Stop();即可。 in the picturebox_click. 在picturebox_click中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM