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

winform 显示动态图片 Gif

时间:2015-07-10 18:15:53      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:

技术分享技术分享

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Diagnostics;

 

namespace DysncPicTest

{

    public partial class Form1 : Form

    {

        private Image m_imgImage = null;

        private EventHandler m_evthdlAnimator = null;

        public Form1()

        {

            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);

         // http://www.cnblogs.com/sosoft/

        }

 

        protected override void OnPaint(PaintEventArgs e)

        {

            base.OnPaint(e);

            if (m_imgImage != null)

            {

                UpdateImage();

                e.Graphics.DrawImage(m_imgImage, new Rectangle(100, 100, m_imgImage.Width, m_imgImage.Height));

            }

        }

 

        protected override void OnLoad(EventArgs e)

        {

            base.OnLoad(e);

            m_imgImage = Image.FromFile("1.gif"); // 加载测试用的Gif图片

            BeginAnimate();

        }

 

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)

        {

             if (m_imgImage != null)

            {

                StopAnimate();

                m_imgImage = null;

            }

        }

 

        private void BeginAnimate()

        {

           if (m_imgImage == null)

                return;

        

           if (ImageAnimator.CanAnimate(m_imgImage))

           {

                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))

            {

                ImageAnimator.UpdateFrames(m_imgImage);

            }

        }

 

        private void OnImageAnimate(Object sender,EventArgs e)

        {

            this.Invalidate();

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

 

        }

    }

}

winform 显示动态图片 Gif

标签:

原文地址:http://www.cnblogs.com/hcyblogs/p/4636122.html

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