代码如下:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using System.Diagnostics; 10 11 namespace DysncPicTest 12 { 13 public partial class Form1 : Form 14 { 15 private Image m_imgImage = null; 16 private EventHandler m_evthdlAnimator = null; 17 public Form1() 18 { 19 InitializeComponent(); 20 this.SetStyle(ControlStyles.UserPaint, true); 21 this.SetStyle(ControlStyles.DoubleBuffer, true); 22 this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); 23 24 m_evthdlAnimator = new EventHandler(OnImageAnimate); 25 Debug.Assert(m_evthdlAnimator != null); 26 // http://www.cnblogs.com/sosoft/ 27 } 28 29 protected override void OnPaint(PaintEventArgs e) 30 { 31 base.OnPaint(e); 32 if (m_imgImage != null) 33 { 34 UpdateImage(); 35 e.Graphics.DrawImage(m_imgImage, new Rectangle(100, 100, m_imgImage.Width, m_imgImage.Height)); 36 } 37 } 38 39 protected override void OnLoad(EventArgs e) 40 { 41 base.OnLoad(e); 42 m_imgImage = Image.FromFile("1.gif"); // 加载测试用的Gif图片 43 BeginAnimate(); 44 } 45 46 private void Form1_FormClosing(object sender, FormClosingEventArgs e) 47 { 48 if (m_imgImage != null) 49 { 50 StopAnimate(); 51 m_imgImage = null; 52 } 53 } 54 55 private void BeginAnimate() 56 { 57 if (m_imgImage == null) 58 return; 59 60 if (ImageAnimator.CanAnimate(m_imgImage)) 61 { 62 ImageAnimator.Animate(m_imgImage,m_evthdlAnimator); 63 } 64 } 65 66 private void StopAnimate() 67 { 68 if (m_imgImage == null) 69 return; 70 71 if (ImageAnimator.CanAnimate(m_imgImage)) 72 { 73 ImageAnimator.StopAnimate(m_imgImage,m_evthdlAnimator); 74 } 75 } 76 77 private void UpdateImage() 78 { 79 if (m_imgImage == null) 80 return; 81 82 if (ImageAnimator.CanAnimate(m_imgImage)) 83 { 84 ImageAnimator.UpdateFrames(m_imgImage); 85 } 86 } 87 88 private void OnImageAnimate(Object sender,EventArgs e) 89 { 90 this.Invalidate(); 91 } 92 93 private void Form1_Load(object sender, EventArgs e) 94 { 95 96 } 97 } 98 }