如何在 Windows 窗体上显示动画 GIF (c#)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/165735/
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
How do you show animated GIFs on a Windows Form (c#)
提问by Stuart Helwig
I have a form showing progress messages as a fairly long process runs. It's a call to a web service so I can't really show a percentage complete figure on a progress bar meaningfully. (I don't particularly like the Marquee property of the progress bar)
我有一个表单显示进度消息作为一个相当长的过程运行。这是对 Web 服务的调用,因此我无法真正有意义地在进度条上显示完整的百分比数字。(我不是特别喜欢进度条的 Marquee 属性)
I would like to show an animated GIF to give the process the feel of some activity (e.g. files flying from one computer to another like Windows copy process).
我想展示一个动画 GIF,让这个过程有一些活动的感觉(例如,文件从一台计算机飞到另一台计算机,如 Windows 复制过程)。
How do you do this?
你怎么做到这一点?
采纳答案by FryHard
It's not too hard.
这不是太难。
- Drop a picturebox onto your form.
- Add the .gif file as the image in the picturebox
- Show the picturebox when you are loading.
- 将图片框拖放到您的表单上。
- 将 .gif 文件添加为图片框中的图像
- 加载时显示图片框。
Things to take into consideration:
需要考虑的事项:
- Disabling the picturebox will prevent the gif from being animated.
- 禁用图片框将阻止 gif 动画。
Animated gifs:
动画 gif:
If you are looking for animated gifs you can generate them here
如果您正在寻找动画 gif,您可以在此处生成它们
Another way of doing it:
另一种方法:
Another way that I have found that works quite well is the async dialog control that I found on the code project
我发现的另一种非常有效的方法是我在代码项目中找到的异步对话框控件
回答by Grank
If you put it in a PictureBox control, it should just work
如果你把它放在一个 PictureBox 控件中,它应该可以正常工作
回答by PhiLho
Note that in Windows, you traditionally don't use animated Gifs, but little AVI animations: there is a Windows native control just to display them. There are even tools to convert animated Gifs to AVI (and vice-versa).
请注意,在 Windows 中,您通常不使用动画 Gif,而是使用很少的 AVI 动画:有一个 Windows 本机控件只是为了显示它们。甚至还有工具可以将动画 Gif 转换为 AVI(反之亦然)。
回答by PhiLho
It doesn't when you start a long operation behind, because everything STOPS since you'Re in the same thread.
当你在后面开始一个长时间的操作时它不会,因为一切都停止了,因为你在同一个线程中。
回答by Aruch
I had the same problem. Whole form (including gif) stopping to redraw itself because of long operation working in the background. Here is how i solved this.
我有同样的问题。由于在后台长时间操作,整个表单(包括 gif)停止重绘自身。这是我解决这个问题的方法。
private void MyThreadRoutine()
{
this.Invoke(this.ShowProgressGifDelegate);
//your long running process
System.Threading.Thread.Sleep(5000);
this.Invoke(this.HideProgressGifDelegate);
}
private void button1_Click(object sender, EventArgs e)
{
ThreadStart myThreadStart = new ThreadStart(MyThreadRoutine);
Thread myThread = new Thread(myThreadStart);
myThread.Start();
}
I simply created another thread to be responsible for this operation. Thanks to this initial form continues redrawing without problems (including my gif working). ShowProgressGifDelegate and HideProgressGifDelegate are delegates in form that set visible property of pictureBox with gif to true/false.
我只是创建了另一个线程来负责这个操作。由于这个初始形式继续重绘而没有问题(包括我的 gif 工作)。ShowProgressGifDelegate 和 HideProgressGifDelegate 是表单中的委托,它们将带 gif 的图片框的可见属性设置为 true/false。
回答by M. Fawad Surosh
I had the same issue and came across different solutions by implementing which I used to face several different issues. Finally, below is what I put some pieces from different posts together which worked for me as expected.
我遇到了同样的问题,并且通过实施我曾经遇到过几个不同的问题,遇到了不同的解决方案。最后,下面是我将来自不同帖子的一些片段放在一起,它们按预期对我有用。
private void btnCompare_Click(object sender, EventArgs e)
{
ThreadStart threadStart = new ThreadStart(Execution);
Thread thread = new Thread(threadStart);
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
Here is the Execution method that also carries invoking the PictureBox control:
下面是 Execution 方法,它也带有调用 PictureBox 控件:
private void Execution()
{
btnCompare.Invoke((MethodInvoker)delegate { pictureBox1.Visible = true; });
Application.DoEvents();
// Your main code comes here . . .
btnCompare.Invoke((MethodInvoker)delegate { pictureBox1.Visible = false; });
}
Keep in mind, the PictureBox is invisible from Properties Window or do below:
请记住,PictureBox 在属性窗口中是不可见的,或者执行以下操作:
private void ComparerForm_Load(object sender, EventArgs e)
{
pictureBox1.Visible = false;
}
回答by Gehan Fernando
Public Class Form1
Private animatedimage As New Bitmap("C:\MyData\Search.gif")
Private currentlyanimating As Boolean = False
Private Sub OnFrameChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.Invalidate()
End Sub
Private Sub AnimateImage()
If currentlyanimating = True Then
ImageAnimator.Animate(animatedimage, AddressOf Me.OnFrameChanged)
currentlyanimating = False
End If
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
AnimateImage()
ImageAnimator.UpdateFrames(animatedimage)
e.Graphics.DrawImage(animatedimage, New Point((Me.Width / 4) + 40, (Me.Height / 4) + 40))
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
BtnStop.Enabled = False
End Sub
Private Sub BtnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStop.Click
currentlyanimating = False
ImageAnimator.StopAnimate(animatedimage, AddressOf Me.OnFrameChanged)
BtnStart.Enabled = True
BtnStop.Enabled = False
End Sub
Private Sub BtnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStart.Click
currentlyanimating = True
AnimateImage()
BtnStart.Enabled = False
BtnStop.Enabled = True
End Sub
End Class