您的位置:首页 > 其它

使用双倍缓存技术降低图象显示速度!

2005-10-23 11:37 633 查看
前段,我在VS,NET环境下学习GDI+,感觉程序显示速度很慢,这我能理解,工具越整合,功能越强大,则越脱离底层。但我不能容忍这么慢的速度,于是我查找相关书籍,找到一个提升显示速度的好办法------使用双倍缓存技术!
让我们来看一个事例来理解双倍缓存技术!
先是不使用缓存技术画图代码片段:
Graphics g = this.CreateGraphics();
Bitmap bitmap=new Bitmap(pictureBox1.Image);
float[][] ptsArray ={
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, 0.5f, 0},
new float[] {0, 0, 0, 0, 1}};
ColorMatrix clrMatrix = new ColorMatrix(ptsArray);
ImageAttributes imgAttributes = new ImageAttributes();
//设置图像的颜色属性
imgAttributes.SetColorMatrix(clrMatrix, ColorMatrixFlag.Default,
ColorAdjustType.Bitmap);
//画图像
g.DrawImage(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height),
0, 0, bitmap.Width, bitmap.Height,
GraphicsUnit.Pixel, imgAttributes);

下面是使用缓存技术来画图片的代码片段:
Bitmap bitmap1=new Bitmap(pictureBox1.Width,pictureBox1.Height);
Graphics g1= Graphics.FromImage(bitmap1);
float[][] ptsArray ={
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, 0.5f, 0},
new float[] {0, 0, 0, 0, 1}};
ColorMatrix clrMatrix = new ColorMatrix(ptsArray);
ImageAttributes imgAttributes = new ImageAttributes();
//设置图像的颜色属性
imgAttributes.SetColorMatrix(clrMatrix, ColorMatrixFlag.Default,
ColorAdjustType.Bitmap);
//画图像
g1.DrawImage(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height),
0, 0, bitmap.Width, bitmap.Height,
GraphicsUnit.Pixel, imgAttributes);
this.pictureBox1.Image=bitmap1;
我大概的解释下,代码意思是先建立一个位图对象,然后Graphics g1= Graphics.FromImage(bitmap1);这很关键,它保证以后用Graphics对象g1绘制图时出现在我们刚建立的位图对象上!接下来的代码是设置所绘制图象的透明属性,我们不用理睬它。最后我们看到这段代:
g1.DrawImage(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height),
0, 0, bitmap.Width, bitmap.Height,
GraphicsUnit.Pixel, imgAttributes);
this.pictureBox1.Image=bitmap1;
实际运行是这样的,g1先将我们要绘制的图形绘制到一个位图图象上,这个位图图象是不可显示的,接下来,我们设置一个可见的picitureBox1用来承载这个不可显示的位图!最终我们可以很方便的建立一个测试程序来判断两种方法的处理效率,可以明显感到:使用双倍缓存技术确实可以提高图形显示速度!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐