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

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

动态图片显示控件----------WinForm控件开发系列的相关文章

走马灯图片轮播控件----------WinForm控件开发系列

/// <summary> /// 走马灯图片轮播控件 /// </summary> [ToolboxItem(true)] [DefaultProperty("Images")] [Description("走马灯图片轮播控件")] public partial class ImageCarouselExt : Control { #region private bool barShow = true; /// <summary>

C#如何用OpenFileDialog控件打开图片显示到PictureBox这个控件

openFileDialog1.Filter = "图片文件|*.jpg|BMP图片|*.bmp|Gif图片|*.gif"; OpenFileDialog ofd = new OpenFileDialog(); ofd.ShowDialog(); picPhoto.Image = Image.FromFile(ofd.FileName);

图片轮播控件----------WinForm控件开发系列

   public partial class ImageCarouselDevelopExt : Control { #region /// <summary> /// 动画播放定时器 /// </summary> private Timer carouselTimer = new Timer(); /// <summary> /// 轮播的五个PictureBox /// </summary> private List<PictureBox>

MAC鱼眼效果菜单控件----------WinForm控件开发系列

/// <summary> /// 鱼眼菜单 /// </summary> [DefaultProperty("Items")] [DefaultEvent("FisheyeItemClick")] [Description("鱼眼菜单")] public partial class FisheyeBarExt : Control { public delegate void EventHandler(object sen

雷达扫描控件----------WinForm控件开发系列

/// <summary> /// 雷达扫描控件 /// </summary> [ToolboxItem(true)] [DefaultProperty("Items")] [Description("雷达扫描控件")] public partial class RadarExt : Control { #region private Color areaColor = Color.LawnGreen; /// <summary>

TabPanel美化控件----------WinForm控件开发系列

/// <summary> /// TabControl /// </summary> [ToolboxItem(true)] [Description("TabControl")] public partial class TabControlExt : TabControl { #region private AnimationTimer animation; /// <summary> /// 动画组件对象 /// </summary&g

Chart实时段分析控件----------WinForm控件开发系列

/// <summary> /// 实时段进度控件 /// </summary> [ToolboxItem(true)] [Description("实时段进度控件")] public partial class ChartExt : Control { #region private Timer interval; /// <summary> /// 定时刷新 /// </summary> [Browsable(false)] [Des

加载等待控件----------WinForm控件开发系列

/// <summary> /// 加载进度控件 /// </summary> [ToolboxItem(true)] [Description("加载进度控件")] [DefaultProperty("Active")] public partial class LoadProgressExt : Control { #region 属性 private LoadProgressType progressType = LoadProgres

温度计控件----------WinForm控件开发系列

/// <summary> /// 温度计控件 /// </summary> [ToolboxItem(true)] [DefaultProperty("Value")] [DefaultEvent("ValueChanged")] [Description("温度计控件")] public partial class ThermometerExt : Control { public delegate void Even