.net C# 动画技术大全,百叶窗,卷动,积木效果,世界之窗,双缓冲技术
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace 图形动画
...{
public partial class Form1 : Form
...{
public Form1()
...{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
...{
this.Left = 0;
this.Width = Screen.AllScreens[0].WorkingArea.Width;
pictureBox1.Left = this.Width - pictureBox1.Width;
this.Height = pictureBox1.Height + 19;
}
private void Form1_Paint(object sender, PaintEventArgs e)
...{
}
//从左到右拉伸显示
int iWidth; //图像宽度
int iHeight; //图像高度
//取得Graphics对象
Graphics g;
Image image;
private void myCreateGraphics()
...{
iWidth = this.pictureBox1.Width; //图像宽度
iHeight = this.pictureBox1.Height; //图像高度
//取得Graphics对象
g = this.pictureBox1.CreateGraphics();
g.Clear(Color.Gray); //初始为全灰色
}
Graphics Coverg;//界面
private void mynewCreateGraphics()
...{
iWidth = this.pictureBox1.Width; //图像宽度
iHeight = this.pictureBox1.Height; //图像高度
//取得Graphics对象
image = new Bitmap(iWidth, iHeight);
g = Graphics.FromImage(image);
Coverg = this.CreateGraphics();
}
private void button1_Click(object sender, EventArgs e)
...{
//从左到右拉伸显示
myCreateGraphics();
for (int x = 0; x <= iWidth; x = x + 2)
...{
g.DrawImage(pictureBox1.Image, 0, 0, x, iHeight);
//Application.DoEvents();
}
}
private void button2_Click(object sender, EventArgs e)
...{
//从上到下拉伸显示
myCreateGraphics();
for (int y = 0; y <= iHeight; y = y + 2)
...{
g.DrawImage(pictureBox1.Image, 0, 0, iWidth, y);
}
}
private void button3_Click(object sender, EventArgs e)
...{
//四周扩散显示
myCreateGraphics();
for (int x = 0; x <= iWidth / 2; x = x + 2)
...{
int y = (int)((iHeight * x / iWidth));
Rectangle DestRect = new Rectangle(iWidth / 2 - x,
iHeight / 2 - y, 2 * x, 2 * y);
Rectangle SrcRect = new Rectangle(0, 0,
pictureBox1.Width, pictureBox1.Height);
g.DrawImage(pictureBox1.Image, DestRect, SrcRect,
GraphicsUnit.Pixel);
}
}
private void button4_Click(object sender, EventArgs e)
...{
//反转图像
myCreateGraphics();
for (int x = -iWidth / 2; x <= iWidth / 2; x = x + 2)
...{
int y = (int)((iHeight * x / iWidth));
Rectangle DestRect = new Rectangle(0, iHeight / 2 - y,
iWidth, 2 * y);
Rectangle SrcRect = new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height);
g.DrawImage(pictureBox1.Image, DestRect, SrcRect, GraphicsUnit.Pixel);
}
}
private void button5_Click(object sender, EventArgs e)
...{
//两边拉伸显示
myCreateGraphics();
for (int y = 0; y <= iWidth / 2; y++)
...{
Rectangle DestRect = new Rectangle(iWidth / 2 - y, 0,
2 * y, iHeight);
Rectangle SrcRect = new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height);
g.DrawImage(pictureBox1.Image, DestRect, SrcRect, GraphicsUnit.Pixel);
}
}
private void button6_Click(object sender, EventArgs e)
...{
//上下对接显示
myCreateGraphics();
int x = 0;
while (x <= iHeight / 2)
...{
Rectangle SrcRect = new Rectangle(0, x, iWidth, 1);
g.DrawImage(pictureBox1.Image, 0, x, SrcRect, GraphicsUnit.Pixel);
SrcRect = new Rectangle(0, iHeight - x, iWidth, 1);
g.DrawImage(pictureBox1.Image, 0, iHeight - x, SrcRect, GraphicsUnit.Pixel);
x++;
}
}
private void button7_Click(object sender, EventArgs e)
...{
//左右对接
myCreateGraphics();
int x = 0;
while (x <= iWidth / 2)
...{
Rectangle SrcRect = new Rectangle(x, 0, 1, iHeight);
g.DrawImage(pictureBox1.Image, x, 0, SrcRect, GraphicsUnit.Pixel);
SrcRect = new Rectangle(iWidth - x, 0, 1, iHeight);
g.DrawImage(pictureBox1.Image, iWidth - x, 0, SrcRect, GraphicsUnit.Pixel);
x++;
}
}
private void button8_Click(object sender, EventArgs e)
...{
int i = 0, j;
//垂直交错
myCreateGraphics();
Rectangle SrcRect;
while (i <= iHeight)
...{
j = i;
while (j > 0)
...{
SrcRect = new Rectangle(0, j - 1, iWidth, 4);
g.DrawImage(pictureBox1.Image, 0, j - 1, SrcRect, GraphicsUnit.Pixel);
SrcRect = new Rectangle(0, iHeight - j, iWidth, 4);
g.DrawImage(pictureBox1.Image, 0, iHeight - j, SrcRect, GraphicsUnit.Pixel);
j = j - 5;
}
i = i + 5;
}
}
private void button9_Click(object sender, EventArgs e)
...{
myCreateGraphics();
int xgroup = 24;
int xcount = iWidth / xgroup;
Rectangle SrcRect;
for (int i = 0; i < xcount; i++)
for (int j = 0; j < xgroup; j++)
...{
SrcRect = new Rectangle(xcount * j + i - 1, 0, 1,
iHeight);
g.DrawImage(pictureBox1.Image, xcount * j + i - 1, 0, SrcRect, GraphicsUnit.Pixel);
//Application.DoEvents();
}
}
private void pictureBox1_Click(object sender, EventArgs e)
...{
}
private void button10_Click(object sender, EventArgs e)
...{
myCreateGraphics();
int i = iHeight;
int k = 60;
Rectangle SrcRect;
while (i > 0)
...{
for (int j = k; j <= i; j = j + 3)
...{
SrcRect = new Rectangle(0, j - k, iWidth, k);
Rectangle DestRect = new Rectangle(0, i - k, iWidth, k);
g.DrawImage(pictureBox1.Image, DestRect, SrcRect, GraphicsUnit.Pixel);
}
i = i - k;
}
}
private void copygline(Bitmap g, Bitmap image, int x1, int y1, int x2, int y2, int step)
...{
//by 闫磊 Email:Landgis@126.com,yanleigis@21cn.com 2007.10.18
int xdelta = x2 - x1;
int ydelta = y2 - y1;
int xstep, ystep;
//Rectangle SrcRect;
if (xdelta < 0)
...{
xdelta = -xdelta;
xstep = -step;
}
else
xstep = step;
if (ydelta < 0)
...{
ydelta = -ydelta;
ystep = -step;
}
else
ystep = step;
int change;
if (xdelta > ydelta)
...{
change = xdelta / 2;
while (x1 != x2)
...{
//SrcRect = new Rectangle(x1, y1, step, step);
g.SetPixel(x1, y1, image.GetPixel(x1, y1));
//g.DrawImage(image, x1, y1, SrcRect, GraphicsUnit.Pixel);
//dest.Pixels[x1, y1] := src.Pixels[x1, y1];
x1 = x1 + xstep;
change = change + ydelta;
if (change > xdelta)
...{
y1 = y1 + ystep;
change = change - xdelta;
}
}
}
else
...{
change = ydelta / 2;
while (y1 != y2)
...{
g.SetPixel(x1, y1, image.GetPixel(x1, y1));
//SrcRect = new Rectangle(x1, y1, step, step);
//g.DrawImage(image, x1, y1, SrcRect, GraphicsUnit.Pixel);
//dest.Pixels[x1, y1] = src.Pixels[x1, y1];
y1 = y1 + ystep;
change = change + xdelta;
if (change > ydelta)
...{
x1 = x1 + xstep;
change = change - ydelta;
}
}
}
}
/**//*
private void copygline(Graphics g, Image image, int x1, int y1, int x2, int y2, int step)
{
int xdelta = x2 - x1;
int ydelta = y2 - y1;
int xstep, ystep;
Rectangle SrcRect;
if (xdelta < 0)
{
xdelta = -xdelta;
xstep = -step;
}
else
xstep = step;
if (ydelta < 0)
{
ydelta = -ydelta;
ystep = -step;
}
else
ystep = step;
int change;
if (xdelta > ydelta)
{
change = xdelta / 2;
while (x1 != x2)
{
SrcRect = new Rectangle(x1, y1, step, step);
g.DrawImage(image, x1, y1, SrcRect, GraphicsUnit.Pixel);
//dest.Pixels[x1, y1] := src.Pixels[x1, y1];
x1 = x1 + xstep;
change = change + ydelta;
if (change > xdelta)
{
y1 = y1 + ystep;
change = change - xdelta;
}
}
}
else
{
change = ydelta / 2;
while (y1 != y2)
{
SrcRect = new Rectangle(x1, y1, step, step);
g.DrawImage(image, x1, y1, SrcRect, GraphicsUnit.Pixel);
//dest.Pixels[x1, y1] = src.Pixels[x1, y1];
y1 = y1 + ystep;
change = change + xdelta;
if (change > ydelta)
{
x1 = x1 + xstep;
change = change - ydelta;
}
}
}
}
*/
private void button11_Click(object sender, EventArgs e)
...{
myCreateGraphics();
int speed = 15;
Rectangle SrcRect, DestRect;
for (int i = 0; i < iHeight; i++)
...{
if (i <= iHeight - speed)
...{
for (int j = 1; j < speed - 1; j++)
...{
SrcRect = new Rectangle(0, i + 10 - j, iWidth, 1);
DestRect = new Rectangle(0, i + j, iWidth, 1);
g.DrawImage(pictureBox1.Image, DestRect, SrcRect, GraphicsUnit.Pixel);
}
}
SrcRect = new Rectangle(0, i, iWidth, 1);
g.DrawImage(pictureBox1.Image, 0, i, SrcRect, GraphicsUnit.Pixel);
}
}
private void button12_Click(object sender, EventArgs e)
...{
int step = 1;
mynewCreateGraphics();
iWidth = iWidth - 2;
iHeight = iHeight - 2;
for (int r = 1; r < iWidth; r = r + step)
...{
/**//* copygline(g, pictureBox1.Image, iWidth - r, 0, iWidth, r * 3 / 4,step);
copygline(g, pictureBox1.Image, 0, iHeight - r * 3 / 4, r, iHeight,step);
copygline(g, pictureBox1.Image, 0, r * 3 / 4, r, 0,step);
copygline(g, pictureBox1.Image, iWidth - r, iHeight, iWidth, iHeight - r * 3 / 4,step);*/
copygline((Bitmap)image, (Bitmap)pictureBox1.Image, iWidth - r, 0, iWidth, r * 3 / 4, step);
copygline((Bitmap)image, (Bitmap)pictureBox1.Image, 0, iHeight - r * 3 / 4, r, iHeight, step);
copygline((Bitmap)image, (Bitmap)pictureBox1.Image, 0, r * 3 / 4, r, 0, step);
copygline((Bitmap)image, (Bitmap)pictureBox1.Image, iWidth - r, iHeight, iWidth, iHeight - r * 3 / 4, step);
if (r == r / 10 * 10)
Coverg.DrawImage(image, 0, 0);
}
//this.CreateGraphics().DrawImage(image, 0, 0);
}
}
}
实现双缓冲的具体步骤
我再来详细解释一下刚才实现双缓冲的具体步骤:
1、在内存中建立一块“虚拟画布”:
Bitmap
bmp = new Bitmap(600, 600);
2、获取这块内存画布的Graphics引用:
Graphics g =
Graphics.FromImage(bmp);
3、在这块内存画布上绘图:
g.FillEllipse(brush, i *
10, j * 10, 10,
10);
4、将内存画布画到窗口中
this.CreateGraphics().DrawImage(bmp, 0, 0);
参考:http://blog.joycode.com/有关内容