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;

namespace Ex03_3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Bitmap bitmap = new Bitmap(Application.StartupPath + "\\Mygif.gif");   //定义Bitmap对象实现,并赋值为指定的GIF动画
        bool current = false;                 //定义布尔型全局变量,并赋值为假


        public void PlayImage()             
        {
            if (!current)             
            {
                ImageAnimator.Animate(bitmap, new EventHandler(this.OnFrameChanged));     //调用Animate方法实现动态播放GIF动画
                current = true;                   //布尔变量current为真
            }
        }

        private void OnFrameChanged(object o, EventArgs e)
        {
            this.Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {

            e.Graphics.DrawImage(this.bitmap, new Point(1, 1));     //绘出GIF动画
            ImageAnimator.UpdateFrames();                              //调用UpdateFrames方法
        }


        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            PlayImage();           //调用PlayImage方法
            ImageAnimator.Animate(bitmap, new EventHandler(this.OnFrameChanged));  //播放GIF动画
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ImageAnimator.StopAnimate(bitmap, new EventHandler(this.OnFrameChanged));  //停止播放GIF动画
        }
    }
}
