一、创建简单动画
动画技术本身十分复杂,本节所指的简单动画是由一幅幅图片构成的。通过对图片的顺序显示,可以构成一种简单的动画。进行本节的动画显示,不仅需要GDI+中的图片表示和显示类,还需要一个Timer组件和打开文件夹对话框组件。
继续上一节创建的项目,添加按钮控件、计时器控件和打开文件夹对话框控件。
编写动画按钮:
///<summary>
///动画
///</summary>
private void button10_Click(object sender,EventArgs e)
{
if(folderBrowserDialog.ShowDialog() == DialogResult.OK)
{
g=splitContainer1.Panel2.CreateGraphics();
string path =folderBrowserDialog.SelectedPath;
files = Directory.GetFiles(path,"*.bmp");
timer.Tick += new EventHandler(timer_Tick);
timer.Enabled =true;
}
}
///<summary>
///每隔一秒响应一次,绘制新图形
///</summary>
void timer_Tick(object sender,EventArgs e)
{
splitContainer1.Panel2.Refresh();
Bitmap b=new Bitmap(files[picNum]);
g.DrawImage(b,0,0);
if(picNum<files.Length-1)
{
picNum++;
}
else
{
picNum=0;
}
}
二、保存窗体图像
在前几节的实例中介绍了很多图像绘制的办法,本节将介绍如何将窗体图像保存到磁盘中,并保存为不同的图片格式。
继续上一节的项目,添加保存按钮
///<summary>
///保存事件
///</summary>
///<param name="sender"></param>
///<param name="e"></param>
private void button11_Click(object sender,EventArgs e)
{
Bitmap bmp = new Bitmap(splitContainer1.Width,splitContainer1.Height);
splitContainer1.DrawToBitmap(bmp,splitContainer1.Bounds);
bmp.Save( @"C:\test.bmp",System.Drawing.Imaging.ImageFormat.Bmp);
}