动态图片显示控件----------WinForm控件开发系列
标签:back Dimension files raw item override protected ati 无法
PictureBox控件无法显示gif格式的图片,该控件利用.NET自带ImageAnimator类来处理图片的帧。
///
/// 动态图片显示控件
///
[ToolboxItem(true)]
[DefaultProperty("Image")]
[Description("动态图片显示控件")]
public partial class AnimationImageExt : Control
{
///
/// 图像的框架维度的属性
///
private FrameDimension frameDimension;
private Image image = null;
///
/// 要显示的图片
///
[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;
///
/// 是否为动态图片
///
[Browsable(false)]
[DefaultValue(false)]
[Description("是否为动态图片")]
public bool IsAnimation
{
get { return this.isAnimation; }
}
private int frameCount = 1;
///
/// 图片总帧数。
///
[Browsable(false)]
[DefaultValue(1)]
[Description("图片总帧数")]
public int FrameCount
{
get { return this.frameCount; }
}
private int currentFrame = 0;
///
/// 当前播放的帧索引
///
[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));
}
}
///
/// 开始循环播放动态图片
///
private void StartAnimation()
{
lock (this.image)
{
ImageAnimator.Animate(this.image, new EventHandler(this.FrameChanged));
}
}
///
/// 停止循环播放动态图片
///
private void StopAnimation()
{
lock (this.image)
{
ImageAnimator.StopAnimate(this.image, new EventHandler(this.FrameChanged));
this.resetProperty();
}
}
///
/// 重置图片信息
///
private void resetProperty()
{
this.frameDimension = null;
this.isAnimation = false;
this.frameCount = 0;
this.currentFrame = -1;
}
///
/// 当前帧更改事件
///
///
///
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
动态图片显示控件----------WinForm控件开发系列
标签:back Dimension files raw item override protected ati 无法
原文地址:https://www.cnblogs.com/tlmbem/p/11223789.html
评论