一、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 ms | 0.1 ms | 5-7 ms |
GDI绘图 | 45-55 ms | 7-9 ms | 40-50 ms |