你仔细看看下面的描述,希望对你有所帮助啊
GDI+在一种与设备无关的环境下提供了一套统一的绘图编程模型,极大的提高了Windows绘图编程的方便性.我编写了一段程序来进行测试。首先,我创建了一个普通的Windows Application,在主Form中,我放置了一个定时器:timer1,然后将它的Interval属性设置为10,然后在Form上放置两个按纽,分别用来控制定时器的开启和关闭,最后,我还放置了一个label控件,用来显示绘图的帧数。
在timer1的timer1_Tick事件中,我写下了如下的代码(其中flag是一个bool型标志变量):
DateTime t1 = DateTime.Now;
Graphics g = this.CreateGraphics();
if(flag)
{
brush = new LinearGradientBrush(new PointF(0.0f, 0.0f),
new PointF(700.0f, 300.0f), Color.Red, Color.Blue);
flag = false;
}
else
{
brush = new LinearGradientBrush(new PointF(0.0f, 0.0f),
new PointF(700.0f, 300.0f), Color.Blue, Color.Red);
flag = true;
}
for(int j = 0; j < 60; j ++)
{
for(int i = 0; i < 60; i++)
{
g.FillEllipse(brush, i * 10, j * 10, 10, 10);
}
}
DateTime t2 = DateTime.Now;
TimeSpan sp = t2 - t1;
float per = 1000 / sp.Milliseconds;
this.label1.Text = "速度:" + per.ToString() + "帧/秒";
改进代码
下面是我改进以后的代码:
DateTime t1 = DateTime.Now;
Bitmap bmp = new Bitmap(600, 600);
Graphics g = Graphics.FromImage(bmp);
if(flag)
{
brush = new LinearGradientBrush(new PointF(0.0f, 0.0f),
new PointF(700.0f, 300.0f), Color.Red, Color.Blue);
flag = false;
}
else
{
brush = new LinearGradientBrush(new PointF(0.0f, 0.0f),
new PointF(700.0f, 300.0f), Color.Blue, Color.Red);
flag = true;
}
for(int j = 0; j < 60; j ++)
{
for(int i = 0; i < 60; i++)
{
g.FillEllipse(brush, i * 10, j * 10, 10, 10);
}
}
this.CreateGraphics().DrawImage(bmp, 0, 0);
DateTime t2 = DateTime.Now;
TimeSpan sp = t2 - t1;
float per = 1000 / sp.Milliseconds;
this.label1.Text = "速度:" + per.ToString() + "帧/秒";
经过改进后,画面刷新速度大大加快,绝对看不到任何的“扫描线”,帧数也从5帧一下就提高到了9帧,几乎是两倍于前的速度。
实现双缓冲的具体步骤
我再来详细解释一下刚才实现双缓冲的具体步骤:
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);
浙公网安备 33010602011771号