【转】C# GDI+ 图像的双缓存技术
2009-08-21 14:46
267 查看
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;
}
}
}
相关文章推荐
- C#: GDI+的绘制图像技术
- C#.net 地图控件开发(四) GDI+图形图像编程基础2.
- 基于VC6实现 GDI+下的双缓存技术
- 在C#用GDI+实现图形图像的任意变形效果(转载)
- c# 图形图像 多媒体技术
- 图像的双缓存技术
- 缓存技术Redis在C#中的使用及Redis的封装
- 基于c#的双缓存技术绘图
- 【转】C#GDI+图像处理
- C# .NET开发图形图像程序时提示"GDI+ 中发生一般性错误"
- [转载]用C#的GDI+技术生成复杂型彩色验证码
- C# (GDI+相关) 图像处理(各种旋转、改变大小、柔化、锐化、雾化、底片、浮雕、黑白、滤镜效果) (转)
- GDI+图形图像处理技术——GDIPlus绘图基础
- 在C#用GDI+实现图形图像的任意变形效果(转载)
- C# 缓存技术(转)
- C#中基于GDI+(Graphics)图像处理系列之文字或者图片水印(透明、任意角度旋转)
- C#中基于GDI+(Graphics)图像处理系列之任意角度旋转图像
- 用C#的GDI+技术生成复杂型彩色验证码
- C++ GDI+ 双缓存技术实现
- C#的双缓存技术