如何在 Windows 窗体(c #)上显示动画 GIF

我有一个表单,显示进度消息作为一个相当长的过程运行。这是一个对 Web 服务的调用,因此我不能真正有意义地在进度条上显示百分比完整数字。(我不是特别喜欢进度条的 Marquee 属性)

我想展示一个动画 GIF,让这个过程有一些活动的感觉(例如文件从一台电脑飞到另一台电脑,就像 Windows 的复制过程)。

你是怎么做到的?

311052 次浏览

如果将其放在 PictureBox 控件中,它应该能够正常工作

这并不难。

  1. 在表单上放置一个图片框。
  2. 添加.gif 文件作为图片框中的图像
  3. 加载时显示图片框。

需要考虑的事项:

  • 禁用图片框将阻止 gif 被动画化。

另一种方法是:

我发现的另一种工作得很好的方法是我在 代码项目上发现的异步对话框控件

请注意,在 Windows 中,您通常不使用动画 Gifs,而是使用小的 AVI 动画: 有一个 Windows 本机控件只是为了显示它们。甚至还有工具可以将动画 Gifs 转换为 AVI (反之亦然)。

当你在后面开始一个长的操作时,它不会停止,因为所有的操作都停止了,因为你在同一个线程中。

我也有同样的问题。整个形式(包括 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();
}

我只是创建了另一个线程来负责这个操作。由于这个初始形式继续重新绘制没有问题(包括我的 gif 工作)。ShowProgressGifgenerate 和 HideProgressGifgenerate 是形式上的委托,它们将 gif 的 pictureBox 的可见属性设置为 true/false。

我遇到了同样的问题,并且通过实施遇到了不同的解决方案,我过去常常面对几个不同的问题。最后,下面是我从不同的职位放在一起的一些片段为我所期望的工作。

private void btnCompare_Click(object sender, EventArgs e)
{
ThreadStart threadStart = new ThreadStart(Execution);
Thread thread = new Thread(threadStart);
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}

下面是 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; });
}

请记住,PictureBox 在“属性”窗口中是不可见的,或者执行以下操作:

private void ComparerForm_Load(object sender, EventArgs e)
{
pictureBox1.Visible = false;
}
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

太晚了,但是!将图像设置为 PictureBox。映像并设置 PictureBox。SizeMode = PictureBoxSizeMode.O 速就行 .Net Framework 4.8

PictureBox.Image = Image.FromFile("location"); // OR From base64 PictureBox.SizeMode = PictureBoxSizeMode.Zoom;