我想显示在.net的Winform一个gif动画。 这该怎么做?
我以前使用VB 6.0。
我想显示在.net的Winform一个gif动画。 这该怎么做?
我以前使用VB 6.0。
把窗体上的图片框,然后指定一个扩展的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#中的用法:
打开一个WinForm项目一拖在PictureBox,一个定时器和一个Button下降,与GifImage.cs
上面显示类。
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();
}
}
@ JeremyThompson的答案开发上我想添加一段代码来向您展示如何实现第一种方法,因为它是简单了很多,而且不需要你手动动画GIF,看到的PictureBox
有一个内置的功能来处理这样的情况。 只需添加PictureBox
到窗体,并在窗体构造函数中指定图像路径PictureBox.ImageLocation
C#
public PictureForm()
{
InitializeComponent();
pictureBoxGif.ImageLocation = "C:\\throbber.gif";
}
VB.Net
Public Sub New()
InitializeComponent()
pictureBoxGif.ImageLocation = "C:\throbber.gif"
End Sub
在我oppinion这是一个更简单的解决方案,特别是人谁是新的.NET。
我打得四处这和播放动画前提是你不要在同一线程上执行其它长期运行的操作。 你执行其它长期运行的操作的那一刻,你会想这样做的另一个线程。
要做到这一点最简单的方法,就是使用BackgroundWorker组件,你可以拖动到您的工具箱形式。 然后你就会把你的长期运行的操作代码的的BackgroundWorker的DoWork的()事件。 最后一步是通过调用BackgroundWorker的实例的RunWorkerAsync()方法来调用你的代码。