码迷,mamicode.com
首页 > Windows程序 > 详细

动态图片显示控件----------WinForm控件开发系列

时间:2019-08-17 13:00:28      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:back   Dimension   files   raw   item   override   protected   ati   无法   

技术图片

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

动态图片显示控件----------WinForm控件开发系列

标签:back   Dimension   files   raw   item   override   protected   ati   无法   

原文地址:https://www.cnblogs.com/tlmbem/p/11223789.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!