解决winform中gif小图片不会闪动问题 2010-7-9 23:00阅读(3)
在项目中加上AnimateImage.cs文件
下面是AnimateImage类的内容
using System; using System.Collections.Generic; using System.Text; using System.Drawing; using System.Drawing.Imaging; namespace Project { /// <summary> /// 表示一类带动画功能的图像。 /// </summary> public class AnimateImage { Image image; FrameDimension frameDimension; /// <summary> /// 动画当前帧发生改变时触发。 /// </summary> public event EventHandler<EventArgs> OnFrameChanged; /// <summary> /// 实例化一个AnimateImage。 /// </summary> /// <param name="img">动画图片。</param> public AnimateImage(Image img) { image = img; lock (image) { mCanAnimate = ImageAnimator.CanAnimate(image); if (mCanAnimate) { Guid[] guid = image.FrameDimensionsList; frameDimension = new FrameDimension(guid[0]); mFrameCount = image.GetFrameCount(frameDimension); } } } bool mCanAnimate; int mFrameCount = 1, mCurrentFrame = 0; /// <summary> /// 图片。 /// </summary> public Image Image { get { return image; } } /// <summary> /// 是否动画。 /// </summary> public bool CanAnimate { get { return mCanAnimate; } } /// <summary> /// 总帧数。 /// </summary> public int FrameCount { get { return mFrameCount; } } /// <summary> /// 播放的当前帧。 /// </summary> public int CurrentFrame { get { return mCurrentFrame; } } /// <summary> /// 播放这个动画。 /// </summary> public void Play() { if (mCanAnimate) { lock (image) { ImageAnimator.Animate(image, new EventHandler(FrameChanged)); } } } /// <summary> /// 停止播放。 /// </summary> public void Stop() { if (mCanAnimate) { lock (image) { ImageAnimator.StopAnimate(image, new EventHandler(FrameChanged)); } } } /// <summary> /// 重置动画,使之停止在第0帧位置上。 /// </summary> public void Reset() { if (mCanAnimate) { ImageAnimator.StopAnimate(image, new EventHandler(FrameChanged)); lock (image) { image.SelectActiveFrame(frameDimension, 0); mCurrentFrame = 0; } } } private void FrameChanged(object sender, EventArgs e) { mCurrentFrame = mCurrentFrame + 1 >= mFrameCount ? 0 : mCurrentFrame + 1; lock (image) { image.SelectActiveFrame(frameDimension, mCurrentFrame); } if (OnFrameChanged != null) { OnFrameChanged(image, e); } } } } AnimateImage类的内容结束----- AnimateImage类的用法: 在图片应用的窗体后台代码中: public partial class Default : Form { AnimateImage image; public Default() { //图片动态显示 image = new AnimateImage(Image.FromFile(Application.StartupPath + "\\" + "Imgs" + "\\" + "02.gif")); image.OnFrameChanged += new EventHandler<EventArgs>(image_OnFrameChanged); SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true); } private void Default_Load(object sender, EventArgs e) { image.Play(); this.pictureBoxBianlukou.ImageLocation = Application.StartupPath + "\\" + "Imgs" + "\\" + "02.gif"; } }
待人以诚,做事用心,对事不对人.