我想在.Net Winform中展示动画gif。 怎么做?
我之前使用的是VB 6.0。
答案 0 :(得分:18)
将一个图片框放在表单上,然后指定一个带有Gif扩展名的图片文件。或者:
以编程方式为gif设置动画图像将帧加载到带有代码的PictureBox中,这里是Gif类:
<强> VB.NET 强>
Public Class GifImage
Private gifImage As Image
Private dimension As FrameDimension
Private frameCount As Integer
Private currentFrame As Integer = -1
Private reverse As Boolean
Private [step] As Integer = 1
Public Sub New(path As String)
gifImage = Image.FromFile(path)
'initialize
dimension = New FrameDimension(gifImage.FrameDimensionsList(0))
'gets the GUID
'total frames in the animation
frameCount = gifImage.GetFrameCount(dimension)
End Sub
Public Property ReverseAtEnd() As Boolean
'whether the gif should play backwards when it reaches the end
Get
Return reverse
End Get
Set
reverse = value
End Set
End Property
Public Function GetNextFrame() As Image
currentFrame += [step]
'if the animation reaches a boundary...
If currentFrame >= frameCount OrElse currentFrame < 1 Then
If reverse Then
[step] *= -1
'...reverse the count
'apply it
currentFrame += [step]
Else
currentFrame = 0
'...or start over
End If
End If
Return GetFrame(currentFrame)
End Function
Public Function GetFrame(index As Integer) As Image
gifImage.SelectActiveFrame(dimension, index)
'find the frame
Return DirectCast(gifImage.Clone(), Image)
'return a copy of it
End Function
End Class
<强> C#强>
public class GifImage
{
private Image gifImage;
private FrameDimension dimension;
private int frameCount;
private int currentFrame = -1;
private bool reverse;
private int step = 1;
public GifImage(string path)
{
gifImage = Image.FromFile(path);
//initialize
dimension = new FrameDimension(gifImage.FrameDimensionsList[0]);
//gets the GUID
//total frames in the animation
frameCount = gifImage.GetFrameCount(dimension);
}
public bool ReverseAtEnd {
//whether the gif should play backwards when it reaches the end
get { return reverse; }
set { reverse = value; }
}
public Image GetNextFrame()
{
currentFrame += step;
//if the animation reaches a boundary...
if (currentFrame >= frameCount || currentFrame < 1) {
if (reverse) {
step *= -1;
//...reverse the count
//apply it
currentFrame += step;
}
else {
currentFrame = 0;
//...or start over
}
}
return GetFrame(currentFrame);
}
public Image GetFrame(int index)
{
gifImage.SelectActiveFrame(dimension, index);
//find the frame
return (Image)gifImage.Clone();
//return a copy of it
}
}
C#用法:
使用上面显示的GifImage.cs
类,在PictureBox,Timer和Button中拖放一个Winform项目。
public partial class Form1 : Form
{
private GifImage gifImage = null;
private string filePath = @"C:\Users\Jeremy\Desktop\ExampleAnimation.gif";
public Form1()
{
InitializeComponent();
//a) Normal way
//pictureBox1.Image = Image.FromFile(filePath);
//b) We control the animation
gifImage = new GifImage(filePath);
gifImage.ReverseAtEnd = false; //dont reverse at end
}
private void button1_Click(object sender, EventArgs e)
{
//Start the time/animation
timer1.Enabled = true;
}
//The event that is animating the Frames
private void timer1_Tick(object sender, EventArgs e)
{
pictureBox1.Image = gifImage.GetNextFrame();
}
}
答案 1 :(得分:11)
开发@ JeremyThompson的答案我想添加一个代码片段来展示如何实现第一种方法,因为它更简单,并且不需要你手动设置gif的动画,看到{{1有一个内置的功能来处理这种情况。
只需在表单中添加PictureBox
,然后在表单构造函数中将图像路径分配给PictureBox
C#
PictureBox.ImageLocation
VB.Net
public PictureForm()
{
InitializeComponent();
pictureBoxGif.ImageLocation = "C:\\throbber.gif";
}
在我看来,这是一个更简单的解决方案,特别是对于.NET新手。
答案 2 :(得分:1)
我已经玩过这个和动画播放,前提是你不在同一个线程上执行另一个长时间运行的操作。当你执行另一个长时间运行的操作时,你想要在另一个线程中执行它。
最简单的方法是使用BackgroundWorker组件,您可以从工具箱中拖动到表单上。然后,您将长时间运行的操作代码放在BackgroundWorker的DoWork()事件中。最后一步是通过调用BackgroundWorker实例的RunWorkerAsync()方法来调用您的代码。