C#中的 ImageAnimator类的应用
ImageAnimator类的应用
运行程序,单击【播放动画】按钮,播放Gif动画,单击【停止播放】按钮,停止播放Gif动画,如图1所示。

图1 ImageAnimator类播放GIF动画
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace _7_04
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Bitmap bitmap = new Bitmap("1.gif");
bool current = False;
public void AnimateImage()
{
if (!current)
{
ImageAnimator.Animate(bitmap, new EventHandler(this.OnFrameChanged));
current = True;
}
}
private void OnFrameChanged(object o, EventArgs e)
{
this.Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
AnimateImage();
ImageAnimator.UpdateFrames();
e.Graphics.DrawImage(this.bitmap, new Point(0, 0));
}
private void button1_Click(object sender, EventArgs e)
{
ImageAnimator.StopAnimate(bitmap, new EventHandler(this.OnFrameChanged));//停止
}
private void button2_Click(object sender, EventArgs e)
{
ImageAnimator.Animate(bitmap, new EventHandler(this.OnFrameChanged));//播放
}
}
}
完整程序代码如下:
★ ★★★★Form1.cs窗体代码文件完整程序代码★★★★★
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace _7_04
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Bitmap bitmap = new Bitmap("1.gif");
bool current = false;
public void AnimateImage()
{
if (!current)
{
ImageAnimator.Animate(bitmap, new EventHandler(this.OnFrameChanged));
current = true;
}
}
private void OnFrameChanged(object o, EventArgs e)
{
this.Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
AnimateImage();
ImageAnimator.UpdateFrames();
e.Graphics.DrawImage(this.bitmap, new Point(0, 0));
}
private void button1_Click(object sender, EventArgs e)
{
ImageAnimator.StopAnimate(bitmap, new EventHandler(this.OnFrameChanged));//停止
}
private void button2_Click(object sender, EventArgs e)
{
ImageAnimator.Animate(bitmap, new EventHandler(this.OnFrameChanged)); //播放
}
}
}
★ ★★★★Form1.designer.cs窗体设计文件完整程序代码★★★★★
namespace _7_04
{
partial class Form1
{
///
/// 必需的设计器变量。
///
private System.ComponentModel.IContainer components = null;
///
/// 清理所有正在使用的资源。
///
/// 如果应释放托管资源,为 true;否则为 false。
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
///
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(539, 139);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "停止播放";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(439, 139);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 1;
this.button2.Text = "播放动画";
this.button2.UseVisualStyleBackColor = true;