using System;
using System.Drawing;
using System.Windows.Forms;
public class animateImage : Form
{
//Create a Bitmpap Object.
Bitmap animatedImage = new Bitmap("SampleAnimation.gif");
bool currentlyAnimating = false;
//This method begins the animation.
public void AnimateImage()
{
if (!currentlyAnimating)
{
//Begin the animation only once.
ImageAnimator.Animate(animatedImage, new EventHandler(this.OnFrameChanged));
currentlyAnimating = true;
}
}
private void OnFrameChanged(object o, EventArgs e)
{
//Force a call to the Paint event handler.
this.Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
//Begin the animation.
AnimateImage();
//Get the next frame ready for rendering.
ImageAnimator.UpdateFrames();
//Draw the next frame in the animation.
e.Graphics.DrawImage(this.animatedImage, new Point(0, 0));
}
public static void Main()
{
Application.Run(new animateImage());
}
}
这里要注意一点:
bitmap一定要放在Paint事件外面 ,否则就重复画一帧;
本文介绍了一个使用C#实现的简单程序,该程序能够在Windows窗体中加载并播放GIF动画。通过实例代码展示了如何利用ImageAnimator类来播放GIF动画,并讲解了关键步骤如初始化动画图片、触发帧变化及重绘等。
1245

被折叠的 条评论
为什么被折叠?



