Form2.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. namespace GDIPlusDemo
  7. {
  8. /// <summary>
  9. /// Form2 的摘要说明。
  10. /// </summary>
  11. public class Form2 : System.Windows.Forms.Form
  12. {
  13. /// <summary>
  14. /// 必需的设计器变量。
  15. /// </summary>
  16. private System.ComponentModel.Container components = null;
  17. Bitmap animatedImage = new Bitmap("SampleAnimation.gif");
  18. bool currentlyAnimating = false;
  19. //This method begins the animation.
  20. public void AnimateImage()
  21. {
  22. if (!currentlyAnimating)
  23. {
  24. //Begin the animation only once.
  25. ImageAnimator.Animate(animatedImage, new EventHandler(this.OnFrameChanged));
  26. currentlyAnimating = true;
  27. }
  28. }
  29. public Form2()
  30. {
  31. //
  32. // Windows 窗体设计器支持所必需的
  33. //
  34. InitializeComponent();
  35. //
  36. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  37. //
  38. }
  39. /// <summary>
  40. /// 清理所有正在使用的资源。
  41. /// </summary>
  42. protected override void Dispose( bool disposing )
  43. {
  44. if( disposing )
  45. {
  46. if(components != null)
  47. {
  48. components.Dispose();
  49. }
  50. }
  51. base.Dispose( disposing );
  52. }
  53. #region Windows 窗体设计器生成的代码
  54. /// <summary>
  55. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  56. /// 此方法的内容。
  57. /// </summary>
  58. private void InitializeComponent()
  59. {
  60. //
  61. // Form2
  62. //
  63. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  64. this.ClientSize = new System.Drawing.Size(664, 390);
  65. this.Name = "Form2";
  66. this.Text = "显示GIF文件";
  67. this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form2_Paint);
  68. }
  69. #endregion
  70. private void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
  71. {
  72. }
  73. private void OnFrameChanged(object o, EventArgs e)
  74. {
  75. //Force a call to the Paint event handler.
  76. this.Invalidate();
  77. }
  78. private void Form2_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
  79. {
  80. //Begin the animation.
  81. AnimateImage();
  82. //Get the next frame ready for rendering.
  83. ImageAnimator.UpdateFrames();
  84. //Draw the next frame in the animation.
  85. e.Graphics.DrawImage(this.animatedImage, new Point(0, 0));
  86. }
  87. }
  88. }