重寫OnPaint事件對表單重繪(顯示gif動畫) 執行個體2

來源:互聯網
上載者:User

標籤:winform   style   blog   http   color   io   使用   ar   for   

    /// <summary>    /// 可顯示Gif  的表單    /// </summary>    public class WinGif : Form    {        private Image _img = null;        public Image Img        {            get { return _img; }            set { _img = value; }        }        private EventHandler evtHandler = null;        public WinGif(Image img)        {            //初始化設定            evtHandler = new EventHandler(OnImageAnimate);            this.Img = img;            this.Width = _img.Width+100;            this.Height = _img.Height + 100;            //開始動畫            BeginAnimate();        }        //委託所關聯的方法        private void OnImageAnimate(Object sender, EventArgs e)        {            //該方法,只是使得當前控制項重繪,調用OnPaint()            this.Invalidate();        }        //設定開始動畫        public void BeginAnimate()        {            if (_img != null)            {                //當gif動畫每隔一定時間後,都會變換一幀,那麼就會觸發一事件                //該方法就是將當前image每變換一幀時,都會調用當前這個委託所關聯的方法                ImageAnimator.Animate(_img,evtHandler);            }        }        //關閉顯示動畫,該方法可以在視窗關閉或者某事件觸發時,已停止渲染當前gif動畫        public void StopAnimate()        {            _img = null;            ImageAnimator.StopAnimate(_img,evtHandler);        }        /// <summary>        /// 重載的當前winform 的OnPaint方法,當介面被沖重繪製時顯示gif的某一幀        /// </summary>        protected override void OnPaint(PaintEventArgs e)        {            base.OnPaint(e);            if (_img != null)            {                //擷取當前gif動畫下一步要渲染的幀                UpdateImage();                //將獲得的當前gif動畫需要渲染的幀顯示在介面的某個位置                e.Graphics.DrawImage(_img,new Rectangle(0,0,_img.Width,_img.Height));            }        }        //獲得當前gif動畫的下一步需要渲染的幀,當下一步任何對當前gif動畫的操作都是對該幀進行操作        private void UpdateImage()        {            ImageAnimator.UpdateFrames(_img);        }    }

使用和結果顯示:

        private void button1_Click(object sender, EventArgs e)        {                       string file = Application.StartupPath + "\\2.gif";            Image img = System.Drawing.Image.FromFile(file);            WinGif gif = new WinGif(img);            gif.Show();        }

重寫OnPaint事件對表單重繪(顯示gif動畫) 執行個體2

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.