Halcon 内存泄漏记录 - C#
- 1. Bitmap 转 HImage
- 2. new 之后要Dispose()
1. Bitmap 转 HImage
由于Bitmap 在转化时使用BitmapData数据,BitmapData数据无法Dispose, 所以在使用时,使用using:
try
{
using (Bitmap b = bmp.Clone() as Bitmap)
{
if (bmp == null)
{
image = null;
return;
};
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData srcBmpData = b.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
HObject _image;
HOperatorSet.GenImage1(out _image, "byte", b.Width, b.Height, srcBmpData.Scan0);
image = new HImage(_image);
_image.Dispose();
b.UnlockBits(srcBmpData);
srcBmpData = null;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.Source);
Console.WriteLine(ex.StackTrace);
image = null;
}
2. new 之后要Dispose()
HObject obj = new HObject(image);
HOperatorSet.ZoomImageFactor(obj, out hoImageZoomed, scaleR, scaleC, "constant");
obj.Dispose();
不能直接使用
HOperatorSet.ZoomImageFactor(new HObject(image), out hoImageZoomed, scaleR, scaleC, "constant");