设置首页收藏本站
开启左侧

如何把显示到的picturebox图片当作hobject变量

[复制链接]
343012090 发表于 2019-9-9 14:19:16 | 显示全部楼层 |阅读模式
显示图片是用 pictureBox1来显示的,它的代码如下,因为获得图像显示后还要进行图像处理,放到halcon里使用Hobject 的图像变量,
但是我写了 Hobject  image= camera.BitMap;却报错,请问如何获得这个图像变量呢?
下面是显示图像的所有代码,
  1. void camera_UpdateImage()
  2.         {
  3.             pictureBox1.Image = camera.BitMap;
  4.         
  5.         }

  6.       public static void CreateBitmap(out Bitmap bitmap, int width, int height, bool color)
  7.         {
  8.             bitmap = new Bitmap(width, height, GetFormat(color));
  9.             if (bitmap.PixelFormat == PixelFormat.Format8bppIndexed)
  10.             {
  11.                 ColorPalette colorPalette = bitmap.Palette;
  12.                 for (int i = 0; i < 256; i++)
  13.                 {
  14.                     colorPalette.Entries[i] = Color.FromArgb(i, i, i);
  15.                 }
  16.                 bitmap.Palette = colorPalette;
  17.             }
  18.         }
  19.         public static void UpdateBitmap(Bitmap bitmap, byte[] buffer, int width, int height, bool color)
  20.         {
  21.             if (!IsCompatible( bitmap, width, height, color))
  22.             {
  23.                 throw new Exception("Cannot update incompatible bitmap.");
  24.             }
  25.             BitmapData bmpData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
  26.             /* Get the pointer to the bitmap's buffer. */            
  27.             IntPtr ptrBmp = bmpData.Scan0;
  28.             /* Compute the width of a line of the image data. */
  29.             int imageStride = GetStride(width, color);
  30.             /* If the widths in bytes are equal, copy in one go. */
  31.             if (imageStride == bmpData.Stride)
  32.             {
  33.                 System.Runtime.InteropServices.Marshal.Copy(buffer, 0, ptrBmp, bmpData.Stride * bitmap.Height );
  34.             }
  35.             else /* The widths in bytes are not equal, copy line by line. This can happen if the image width is not divisible by four. */
  36.             {
  37.                 for (int i = 0; i < bitmap.Height; ++i)
  38.                 {
  39.                     Marshal.Copy(buffer, i * imageStride, new IntPtr(ptrBmp.ToInt64() + i * bmpData.Stride), width);
  40.                 }
  41.             }
  42.             /* Unlock the bits. */
  43.             bitmap.UnlockBits(bmpData);
  44.         }
  45.     }
复制代码
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
ChrisKhoo 发表于 2019-9-10 09:25:45 | 显示全部楼层
camera.BitMap 是什么类型的变量?如果不属于Halcon的变量,需要做conversion。利用gen_image之类的算子。
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表