Jia_ShengJie

有你的地方、才是家;

自定义控件 播放GIF动画

代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//debug 引用
using System.Diagnostics;

namespace CYSoft.TS.UI.StudentInfo
{
    public partial class PicboxPlayGif : UserControl
    {
        private Image m_imgImage = null;
        private EventHandler m_evthdlAnimator = null;     
        public PicboxPlayGif()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            //为委托关联一个处理方法
            m_evthdlAnimator = new EventHandler(OnImageAnimate);
            Debug.Assert(m_evthdlAnimator != null);
        
        }

        private bool isPicboxFit = true;
        [Description("pic 的宽和高和 真实gif的宽和高 是否要一致 ture=一致  false=不一致 ")]
        public bool _isPicboxFit {
            get { return isPicboxFit; }
            set { this.isPicboxFit = value;
            this.Invalidate();
            }
        }

        private int picWidth = 0;
        [Description("图片宽度(如果isPicboxFit=true 这个参数无意义)")]
        public int _picWidth {
            get { return picWidth; }
            set {
                if (!isPicboxFit) {
                    if(value!=0)
                    {
                    this.picWidth = value;
                    this.Invalidate();
                    }
                }
            }
        }

        private int picHeight = 0;
       [Description("图片高度(如果isPicboxFit=true 这个参数无意义)")]
        public int _picHeight {
            get { return picHeight; }
            set {
                if (!isPicboxFit)
                {
                    if (value != 0)
                    {
                        this.picHeight = value;
                        this.Invalidate();
                    }
                }
            }
        }

        private string imagePath = "C:\\Users\\Thinkpad\\Desktop\\素材\\WaitLoading.gif";
        [Description("图片路径")]
        public string _imagePath {
            get { return imagePath; }
            set { this.imagePath = value;
            this.Invalidate();
            }
        }

        private AA imageLayout = AA.Stretch;
        [Description("图片在picbox中的显示方式")]
        public AA _imageLayout {
            get { return imageLayout; }
            set {
                this.imageLayout = value;
                this.Invalidate();
            }
        }

        public enum AA { 
           None,
            Title,
            Center,
            Stretch,
            Zoom
        }


        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            if (m_imgImage != null)
            {
                UpdateImage();
                //不用picbox 直接输出
               // e.Graphics.DrawImage(m_imgImage, new Rectangle(100, 100, m_imgImage.Width, m_imgImage.Height));
               
                //image 格式
                if(imageLayout==AA.None)
                {
                    pic.BackgroundImageLayout = ImageLayout.None;
                }else if(imageLayout==AA.Title)
                {
                    pic.BackgroundImageLayout = ImageLayout.Tile;
                }
                else if (imageLayout == AA.Stretch)
                {
                    pic.BackgroundImageLayout = ImageLayout.Stretch;
                }
                else if (imageLayout == AA.Zoom)
                {
                    pic.BackgroundImageLayout = ImageLayout.Zoom;
                }
                else {
                    pic.BackgroundImageLayout = ImageLayout.Center;
                }

                pic.BackgroundImage = m_imgImage;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            //加载图片
            m_imgImage = Image.FromFile(imagePath);
            if (isPicboxFit)
            {
                this.Width = m_imgImage.Width;
                this.Height = m_imgImage.Height;
            }
            else
            {
                this.Width = picWidth;
                this.Height = picHeight;
            }

            //pic设置
            pic.BackColor = Color.Transparent;
            pic.Dock = DockStyle.Fill;

            //BeginAnimate();
        }

        /// <summary>
        /// 播放或停止 动画
        /// </summary>
        /// <param name="isPlay">true=播放 false=停止</param>
        public void _PlayOrEndGif(bool isPlay) {
            if (isPlay)
            {                
                BeginAnimate();
            }
            else {
                if (m_imgImage != null)
                {
                    StopAnimate();
                    //m_imgImage = null;
                }
            }
        }

        //开始动画
        private void BeginAnimate()
        {
           if (m_imgImage == null)
                return;
         
           if (ImageAnimator.CanAnimate(m_imgImage))
           {
               //当gif动画每隔一定时间后,都会变换一帧,那么就会触发一事件,
               //该方法就是将当前image每变换一帧时,都会调用当前这个委托所关联的方法。
                ImageAnimator.Animate(m_imgImage,m_evthdlAnimator);
           }
        }
 
        //结束动画
        private void StopAnimate()
        {
            if (m_imgImage == null)
                return;
 
            if (ImageAnimator.CanAnimate(m_imgImage))
            {
                ImageAnimator.StopAnimate(m_imgImage,m_evthdlAnimator);
            }
        }
 
        //切换图片(帧图片)
        private void UpdateImage()
        {
            if (m_imgImage == null)
                return;
 
            if (ImageAnimator.CanAnimate(m_imgImage))
            {
                //获得当前gif动画的下一步需要渲染的帧,当下一步任何对当前gif动画的操作都是对该帧进行操作)
                ImageAnimator.UpdateFrames(m_imgImage);
            }
        }

        
        private void OnImageAnimate(Object sender,EventArgs e)
        {
            //使得当前这个winfor重绘,然后去调用该winform的OnPaint()方法进行重绘
            this.Invalidate(); 
        }

        private void PicboxPlayGif_Load(object sender, EventArgs e)
        {

        }
    }
}


调用:

 

上图右边是属性设置:

播放gif:

picboxPlayGif2._PlayOrEndGif(true);

停止gif:

picboxPlayGif2._PlayOrEndGif(true);

 

阅读更多
版权声明:此文只供交流、学习;如若转载、请声明出处: Jia_ShengJie(http://blog.csdn.net/u013816709?viewmode=contents) https://blog.csdn.net/u013816709/article/details/47414867
个人分类: C#
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭