最近做项目有用到Halcon图像转成Bitmap格式的情况,在网上搜索部分代码整理调试了一下,效果不错,在博客中记录一下。
1、灰度图转Bitmap
//灰度图HObject转Bitmap
[DllImport("kernel32.dll")]
public static extern void CopyMemory(int Destination, int add, int Length);
private Bitmap HObjectToBitmap(HObject ho_Image)
{
try
{
HOperatorSet.GetImagePointer1(ho_Image, out HTuple pointer, out HTuple type, out HTuple width, out HTuple height);
Bitmap bmpImage = new Bitmap(width.I, height.I, PixelFormat.Format8bppIndexed);
ColorPalette pal = bmpImage.Palette;
for (int i = 0; i < 256; i++)
{
pal.Entries[i] = Color.FromArgb(255, i, i, i);
}
bmpImage.Palette = pal;
BitmapData bitmapData = bmpImage.LockBits(new Rectangle(0, 0, width, height), ImageL