PictureBox控件无法显示gif格式的图片,该控件利用.NET自带ImageAnimator类来处理图片的帧。
/// <summary> /// 动态图片显示控件 /// </summary> [ToolboxItem(true)] [DefaultProperty("Image")] [Description("动态图片显示控件")] public partial class AnimationImageExt : Control { /// <summary> /// 图像的框架维度的属性 /// </summary> private FrameDimension frameDimension; private Image image = null; /// <summary> /// 要显示的图片 /// </summary> [Browsable(true)] [DefaultValue(null)] [Description("要显示的图片")] public Image Image { get { return this.image; } set { if (this.image == null && value == null) return; if (value == null)//清除图片 { if (this.isAnimation) { this.StopAnimation(); } this.image = value; this.Invalidate(); } else//加载图片 { if (this.isAnimation) { this.StopAnimation(); } this.image = value; lock (this.image) { this.isAnimation = ImageAnimator.CanAnimate(this.image); if (this.isAnimation)//gif图片 { Guid[] guid = this.image.FrameDimensionsList; this.frameDimension = new FrameDimension(guid[0]); this.frameCount = this.image.GetFrameCount(this.frameDimension); this.currentFrame = 0; this.image.SelectActiveFrame(this.frameDimension, this.currentFrame); this.Invalidate(); this.StartAnimation(); } else//普通图片 { this.frameCount = 1; this.currentFrame = 0; this.Invalidate(); } } } } } private bool isAnimation; /// <summary> /// 是否为动态图片 /// </summary> [Browsable(false)] [DefaultValue(false)] [Description("是否为动态图片")] public bool IsAnimation { get { return this.isAnimation; } } private int frameCount = 1; /// <summary> /// 图片总帧数。 /// </summary> [Browsable(false)] [DefaultValue(1)] [Description("图片总帧数")] public int FrameCount { get { return this.frameCount; } } private int currentFrame = 0; /// <summary> /// 当前播放的帧索引 /// </summary> [Browsable(false)] [DefaultValue(0)] [Description("当前播放的帧数")] public int CurrentFrame { get { return this.currentFrame; } } protected override Size DefaultSize { get { return new Size(100, 100); } } public AnimationImageExt() { SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.OptimizedDoubleBuffer, true); SetStyle(ControlStyles.ResizeRedraw, true); SetStyle(ControlStyles.SupportsTransparentBackColor, true); InitializeComponent(); this.BackColor = Color.Transparent; } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); if (this.image != null) { Graphics g = e.Graphics; g.DrawImage(this.image, new Point(0, 0)); } } /// <summary> /// 开始循环播放动态图片 /// </summary> private void StartAnimation() { lock (this.image) { ImageAnimator.Animate(this.image, new EventHandler(this.FrameChanged)); } } /// <summary> /// 停止循环播放动态图片 /// </summary> private void StopAnimation() { lock (this.image) { ImageAnimator.StopAnimate(this.image, new EventHandler(this.FrameChanged)); this.resetProperty(); } } /// <summary> /// 重置图片信息 /// </summary> private void resetProperty() { this.frameDimension = null; this.isAnimation = false; this.frameCount = 0; this.currentFrame = -1; } /// <summary> /// 当前帧更改事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void FrameChanged(object sender, EventArgs e) { this.currentFrame = this.currentFrame + 1 >= this.frameCount ? 0 : this.currentFrame + 1; lock (this.image) { this.image.SelectActiveFrame(this.frameDimension, this.currentFrame); this.Invalidate(); } } }
源码下载:动态图片显示控件.zip
原文地址:https://www.cnblogs.com/tlmbem/p/11223789.html
时间: 2024-11-05 17:19:14