【转】C# GDI+ 图像的双缓存技术
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace DoubleBuffer
{
public partial class Form1 : Form
{
bool flag = true;
Brush brush;
public Form1()
{
InitializeComponent();
}
private void timerTest_Tick(object sender, EventArgs e)
{
if (rbYes.Checked)//默认
{
BasicDraw();
}
if(rbUser.Checked)//自己实现双缓冲原理
UseMyImage();
if (rbManual.Checked)//手动管理双缓冲
{
UseDoubleBuffer();
}
}
private void BasicDraw()
{
DateTime t1 = DateTime.Now;
Graphics g = this.CreateGraphics();
Draw(g);
DateTime t2 = DateTime.Now;
TimeSpan sp = t2 - t1;
float per = 1000 / sp.Milliseconds;
lbSpeed.Text = "速度:" + per.ToString() + "帧/秒";
}
private void UseMyImage()
{
DateTime t1 = DateTime.Now;
Bitmap bmp = new Bitmap(600, 600);
Graphics g = Graphics.FromImage(bmp);
Draw(g);
this.CreateGraphics().DrawImage(bmp, 0, 0);
DateTime t2 = DateTime.Now;
TimeSpan sp = t2 - t1;
float per = 1000 / sp.Milliseconds;
lbSpeed.Text = "速度:" + per.ToString() + "帧/秒";
}
private void UseDoubleBuffer()
{
BufferedGraphicsContext currentContext = BufferedGraphicsManager.Current;
BufferedGraphics myBuffer = currentContext.Allocate(this.CreateGraphics(), this.DisplayRectangle);
DateTime t1 = DateTime.Now;
Graphics g = myBuffer.Graphics;
g.Clear(Button.DefaultBackColor);
Draw(g);
myBuffer.Render();
DateTime t2 = DateTime.Now;
TimeSpan sp = t2 - t1;
float per = 1000 / sp.Milliseconds;
lbSpeed.Text = "速度:" + per.ToString() + "帧/秒";
myBuffer.Dispose();
}
private void Draw(Graphics g)
{
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);
}
}
}
private void btnStart_Click(object sender, EventArgs e)
{
timerTest.Enabled = true;
}
private void btnStop_Click(object sender, EventArgs e)
{
timerTest.Enabled = false;
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace DoubleBuffer
{
public partial class Form1 : Form
{
bool flag = true;
Brush brush;
public Form1()
{
InitializeComponent();
}
private void timerTest_Tick(object sender, EventArgs e)
{
if (rbYes.Checked)//默认
{
BasicDraw();
}
if(rbUser.Checked)//自己实现双缓冲原理
UseMyImage();
if (rbManual.Checked)//手动管理双缓冲
{
UseDoubleBuffer();
}
}
private void BasicDraw()
{
DateTime t1 = DateTime.Now;
Graphics g = this.CreateGraphics();
Draw(g);
DateTime t2 = DateTime.Now;
TimeSpan sp = t2 - t1;
float per = 1000 / sp.Milliseconds;
lbSpeed.Text = "速度:" + per.ToString() + "帧/秒";
}
private void UseMyImage()
{
DateTime t1 = DateTime.Now;
Bitmap bmp = new Bitmap(600, 600);
Graphics g = Graphics.FromImage(bmp);
Draw(g);
this.CreateGraphics().DrawImage(bmp, 0, 0);
DateTime t2 = DateTime.Now;
TimeSpan sp = t2 - t1;
float per = 1000 / sp.Milliseconds;
lbSpeed.Text = "速度:" + per.ToString() + "帧/秒";
}
private void UseDoubleBuffer()
{
BufferedGraphicsContext currentContext = BufferedGraphicsManager.Current;
BufferedGraphics myBuffer = currentContext.Allocate(this.CreateGraphics(), this.DisplayRectangle);
DateTime t1 = DateTime.Now;
Graphics g = myBuffer.Graphics;
g.Clear(Button.DefaultBackColor);
Draw(g);
myBuffer.Render();
DateTime t2 = DateTime.Now;
TimeSpan sp = t2 - t1;
float per = 1000 / sp.Milliseconds;
lbSpeed.Text = "速度:" + per.ToString() + "帧/秒";
myBuffer.Dispose();
}
private void Draw(Graphics g)
{
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);
}
}
}
private void btnStart_Click(object sender, EventArgs e)
{
timerTest.Enabled = true;
}
private void btnStop_Click(object sender, EventArgs e)
{
timerTest.Enabled = false;
}
}
}