C#图像显示


一、PictureBox采用直接赋值Image的方式进行显示图像

private void DisplayImage(PictureBox pb, Bitmap bitmap)
{
     //判断 bitmap非空,并且未被 Dispose() 后进行赋值显示
     if (bitmap != null && bitmap.PixelFormat != PixelFormat.DontCare)
     {
         if (pb.Image != null)
             pb.Image.Dispose();//非空时,释放掉原有的图像内存,否则软件内存增涨
         pb.Image = bitmap.Clone() as Bitmap;//将 Bitmap 的克隆复本赋给 PictureBox 的 Image
     }
  }

二、PictureBox采用GDI绘图

//使用GDI绘图
private Graphics g1 = null;

private void DisplayImage(PictureBox pb, Bitmap bitmap)
{
    /* 使用GDI绘图 */
    if (g1 == null)
        g1 = pb.CreateGraphics();
    g1.DrawImage(bitmap, new Rectangle(0, 0, pb.Width, pb.Height),
                new Rectangle(0, 0, bitmap.Width, bitmap.Height), GraphicsUnit.Pixel);
}

三、两种方式的效果及速度对比

采用直接赋值Image的方式,拖动界面时,图像不会消失;
而采用GDI绘图时,界面变化,图像就会消失。

时间对比:

4096*4096 灰度图像512*512 灰度图像4096*4096 RGB图像
直接赋值2-3 ms0.1 ms5-7 ms
GDI绘图45-55 ms7-9 ms40-50 ms

四、相关的测试程序及图像

测试demo资源下载链接

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值