Conversion between HALCON Hobject and Bitmap

Grayscale HObject to Bitmap

public static Bitmap HObject2Bitmap(HObject ho)
{<!-- -->
    try
    {<!-- -->
        HTuple type, width, height, pointer;
        //HOperatorSet.AccessChannel(ho, out ho, 1);
        HOperatorSet.GetImagePointer1(ho, out pointer, out type, out width, out height);
        //himg.GetImagePointer1(out type, out width, out height);
        Bitmap bmp = new Bitmap(width.I, height.I, PixelFormat.Format8bppIndexed);
        ColorPalette pal = bmp.Palette;
        for (int i = 0; i <= 255; i ++ )
        {<!-- -->
            pal.Entries[i] = Color.FromArgb(255, i, i, i);
        }
        bmp.Palette = pal;
        BitmapData bitmapData = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
        int PixelSize = Bitmap.GetPixelFormatSize(bitmapData.PixelFormat) / 8;
        int stride = bitmapData.Stride;
        int ptr = bitmapData.Scan0.ToInt32();
        for (int i = 0; i < height; i ++ )
        {<!-- -->
            CopyMemory(ptr, pointer, width * PixelSize);
            pointer += width;
            ptr += bitmapData.Stride;
        }
        bmp. UnlockBits(bitmapData);
        return bmp;
    }
    catch (Exception exc)
    {<!-- -->
        return null;
    }
}

HObject to HImage of grayscale image

public HImage HObject2HImage1(HObject hObj)
{<!-- -->
    HImage image = new HImage();
    HTuple type, width, height, pointer;
    HOperatorSet.GetImagePointer1(hObj, out pointer, out type, out width, out height);
    image.GenImage1(type, width, height, pointer);
    return image;
}

Bitbmp to Hobject

public void BitmapToHobject(Bitmap bitmap, out HObject image)
        {<!-- -->
            int height = bitmap.Height;//The height of the image
            int width = bitmap.Width;//The width of the image


            Rectangle imgRect = new Rectangle(0, 0, width, height);
            BitmapData bitData = bitmap.LockBits(imgRect, ImageLockMode.ReadOnly, bitmap.PixelFormat);


            //Because the number of bytes in each line of the Bitmap image must be kept as a multiple of 4, when the number of bytes in the line does not meet this condition, the line will be supplemented, and the stride number Stride represents the number of each line after supplementation The number of bytes, also known as the scan width
            int stride = bitData.Stride;
            switch (bitmap. PixelFormat)
            {<!-- -->
                case PixelFormat.Format8bppIndexed:
                    {<!-- -->
                        unsafe
                        {<!-- -->
                            int count = height * width;
                            byte[] data = new byte[count];
                            byte* bptr = (byte*)bitData.Scan0;
                            fixed (byte* pData = data)
                            {<!-- -->
                                for (int i = 0; i < height; i ++ )
                                {<!-- -->
                                    for (int j = 0; j < width; j ++ )
                                    {<!-- -->
                                        /*
                                         *
                                         If you use GenImage1 directly and pass in BitData's Scan0 (the pointer to the first element of the image) as a memory pointer, if the image does not satisfy the multiple of 4, then the filled part will also participate, resulting in image distortion
                                         *
                                         *
                                         */


                                        // discard the filled part
                                        data[i * width + j] = bptr[i * stride + j];
                                    }

                                }
                                HOperatorSet.GenImage1(out image, "byte", width, height, new IntPtr(pData));
                            }
                        }

                    }
                    break;
                case PixelFormat.Format24bppRgb:
                    {<!-- -->
                        unsafe
                        {<!-- -->
                            int count = height * width * 3;//24-bit BitMap has three bytes per pixel
                            byte[] data = new byte[count];
                            byte* bptr = (byte*)bitData.Scan0;
                            fixed (byte* pData = data)
                            {<!-- -->
                                for (int i = 0; i < height; i ++ )
                                {<!-- -->
                                    for (int j = 0; j < width * 3; j++ )
                                    {<!-- -->
                                        //The pixels of each channel need to correspond one by one
                                        data[i * width * 3 + j] = bptr[i * stride + j];
                                    }
                                }
                                HOperatorSet.GenImageInterleaved(out image, new IntPtr(pData), "bgr", bitmap.Width, bitmap.Height, 0, "byte", bitmap.Width, bitmap.Height, 0, 0, -1, 0);
                            }
                        }
                    }
                    break;
                default:
                    {<!-- -->
                        unsafe
                        {<!-- -->
                            int count = height * width;
                            byte[] data = new byte[count];
                            byte* bptr = (byte*)bitData.Scan0;
                            fixed (byte* pData = data)
                            {<!-- -->
                                for (int i = 0; i < height; i ++ )
                                {<!-- -->
                                    for (int j = 0; j < width; j ++ )
                                    {<!-- -->
                                        data[i * width + j] = bptr[i * stride + j];
                                    }

                                }
                                HOperatorSet.GenImage1(out image, "byte", width, height, new IntPtr(pData));
                            }
                        }

                    }
                    break;
            }
            bitmap. UnlockBits(bitData);
        }
    }