当前位置: 移动技术网 > IT编程>开发语言>c# > Winform 显示Gif图片的实例代码

Winform 显示Gif图片的实例代码

2019年07月18日  | 移动技术网IT编程  | 我要评论
复制代码 代码如下:using system;using system.collections.generic;using system.componentmodel;us

复制代码 代码如下:

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

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

        }
    }
}        

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网