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

        前段,我在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用来承载这个不可显示的位图!最终我们可以很方便的建立一个测试程序来判断两种方法的处理效率,可以明显感到:使用双倍缓存技术确实可以提高图形显示速度!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值