在 X 时间内在 C# 中加载动画 GIF 图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18548655/
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
Load animated GIF image in C# during X time
提问by mafap
How can I show an animated imagein 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;
}
采纳答案by devavx
To display an animated image on your Form,do the following;
要在您的表单上显示动画图像,请执行以下操作;
1.)Drop a PictureBox on your Form.
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.
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.
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.
Anytime during the course of execution of the project if you want to change the image,use the statement below;
就是这样,现在尝试运行该项目,如果没有抛出异常,您将在PictureBox中看到您的图像动画。
在项目执行过程中的任何时候,如果您想更改图像,请使用以下声明;
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.
Drop a Timer on your Form.
Set its Interval property to 5000 Milliseconds.
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).
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 :
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;
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;
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.
那应该可以完成工作。还有其他事情,请告诉我。
回答by Mothy
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 窗体中将不会出现任何图像动画。
回答by rtj3000
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.
添加一个图片框并将图像设置为您的 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; timer1.Stop(); (prevent the timer starting on load up)
返回您的表单并双击您的计时器,并添加 pictureBox1.Visible = false; timer1.Stop(); (防止定时器在加载时启动)
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 开始单击已初始化写入的私有无效按钮,请添加 pictureBox1.Visible = false;
now add your button and code the following to button1_click
现在添加您的按钮并将以下代码添加到 button1_click
pictureBox1.Visible = true; Timer1.Start(); (this is the code used to start the timer upon button click) enter code here
pictureBox1.Visible = true; Timer1.Start(); (这是用于在按钮单击时启动计时器的代码)在此处输入代码
IF THIS CAUSES A LOOPING ERROR i.e. gif does not hide, simply put timer.Stop(); in the picturebox_click.
如果这导致循环错误,即 gif 没有隐藏,只需将 timer.Stop(); 在picturebox_click 中。