Halcon图像与Bitmap相互转化
 public class HImageHelper
    {
    public static Bitmap HImage2RGBBitmap(HObject ho_image)
    {
      HTuple width0, height0, type, width, height;
      //获取图像尺寸
      HOperatorSet.GetImageSize(ho_image, out width0, out height0);
      //创建交错格式图像
      HOperatorSet.InterleaveChannels(ho_image, out HObject InterImage, "argb", "match", 255);  
      HOperatorSet.GetImagePointer1(InterImage, out HTuple Pointer, out type, out width, out height);
      IntPtr ptr = Pointer;
      Bitmap res32 = new Bitmap(width / 4, height, width, PixelFormat.Format32bppArgb, ptr);  
      Bitmap res24 = new Bitmap(res32.Width, res32.Height, PixelFormat.Format24bppRgb);
      Graphics graphics = Graphics.FromImage(res24);
      graphics.DrawImage(res32, new Rectangle(0, 0, res32.Width, res32.Height));
      res32.Dispose();
      return res24;
    }
    public static Bitmap HImage2GrayBitmap(HObject image)
    {
      try
      {
        HOperatorSet.GetImagePointer1(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;
Int32 newDataWidth = ((System.Drawing.Image.GetPixelFormatSize(PixelFormat.Format8bppIndexed) * width) + 7) / 8;
        int stride = pixelSize * width;
        IntPtr dst = bitmapData.Scan0;
        IntPtr src = pointer;
        int length = Math.Abs(bitmapData.Stride) * bmpImage.Height;
        if (stride % 4 == 0)
        {
          byte[] data = new byte[length];
          System.Runtime.InteropServices.Marshal.Copy(src, data, 0, length);
          System.Runtime.InteropServices.Marshal.Copy(data, 0, dst, length);
        }
        else
        {
          stride = stride + 4 - stride % 4;
          int length2 = width.I * pixelSize;
          int count = length / stride;
          for (int i = 0; i < count; i++)
          {
            byte[] data = new byte[length2];
            System.Runtime.InteropServices.Marshal.Copy(src, data, 0, length2);
            System.Runtime.InteropServices.Marshal.Copy(data, 0, dst, length2);
            src += length2;
            dst += stride;
          }
        }
        bmpImage.UnlockBits(bitmapData);
        return bmpImage;
      }
      catch (Exception)
      {
        return null;
      }
    }
    public static HObject Bitmap2HImageColor(Bitmap bmp)
    {
      HObject image = new HObject();
      try
      {
        Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
        BitmapData srcBmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
        HOperatorSet.GenImageInterleaved(out image, srcBmpData.Scan0, "bgr", bmp.Width, bmp.Height, 0, "byte", 0, 0, 0, 0, -1, 0);
        bmp.UnlockBits(srcBmpData);
      }
      catch (Exception ex)
      {
        image = null;
      }
      finally
      {
      }
      return image;
    }
    public static HObject Bitmap2HImageGray(Bitmap bmp)
    {
      HObject image = new HObject();
      try
      {
        Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData srcBmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
        HOperatorSet.GenImage1(out image, "byte", bmp.Width, bmp.Height, srcBmpData.Scan0);
        bmp.UnlockBits(srcBmpData);
      }
      catch (Exception ex)
      {
        image = null;
      }
      return image;
    }
}
------------------------------------
承接
**视觉检测软件开发及调试
**工业软件开发
**上位机软件开发
wechat:luoran2024
qq:565934058
email:taoyuansu@qq.com
海量教育资源及影视资源下载
微信公众号:EFun科技
------------------------------------
 
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号