Halcon中HObject图像转Bitmap

最近做项目有用到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), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
                int pixelSize = Bitmap.GetPixelFormatSize(bitmapData.PixelFormat) / 8;
                int stride = bitmapData.Stride;
                int ptr = bitmapData.Scan0.ToInt32();
                if (width % 4 == 0)
                    CopyMemory(ptr, pointer, width * height * pixelSize);
                else
                {
                    for (int i = 0; i < height; i++)
                    {
                        CopyMemory(ptr, pointer, width * pixelSize);
                        pointer += width;
                        ptr += bitmapData.Stride;
                    }
                }
                bmpImage.UnlockBits(bitmapData);
                return bmpImage;
            }
            catch (Exception)
            {
                return null;
            }
        }

2、彩色图转Bitmap

        private Bitmap HObjectToBitmap(HObject ho_Image)
        {
            try
            {
                HOperatorSet.GetImagePointer3(ho_Image, out HTuple hv_Red, out HTuple hv_Green, out HTuple hv_Blue, out HTuple type, out HTuple width, out HTuple height);
                Bitmap bmpImage = new Bitmap(width.I, height.I, PixelFormat.Format32bppArgb);

                BitmapData bitmapData = bmpImage.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
                unsafe
                {
                    byte* bptr = (byte*)bitmapData.Scan0;
                    byte* r = ((byte*)hv_Red.I);
                    byte* g = ((byte*)hv_Green.I);
                    byte* b = ((byte*)hv_Blue.I);


                    int length = width * height;
                    for (int i = 0; i < length; i++)
                    {
                        bptr[i * 4] = (b)[i];
                        bptr[i * 4 + 1] = (g)[i];
                        bptr[i * 4 + 2] = (r)[i];
                        bptr[i * 4 + 3] = 255;
                    }
                }
                bmpImage.UnlockBits(bitmapData);

            //32位Bitmap转24位
            Bitmap bmpImage24 = new Bitmap(bmpImage.Width, bmpImage.Height,PixelFormat.Format24bppRgb);
            Graphics graphics = Graphics.FromImage(bmpImage24);
            graphics.DrawImage(bmpImage, new Rectangle(0, 0, bmpImage.Width,bmpImage.Height));

            return bmpImage24;
            }
            catch (Exception)
            {
                return null;
            }
        }

你可能感兴趣的:(C#,c#)