显示图片是用 pictureBox1来显示的,它的代码如下,因为获得图像显示后还要进行图像处理,放到halcon里使用Hobject 的图像变量,
但是我写了 Hobject image= camera.BitMap;却报错,请问如何获得这个图像变量呢?
下面是显示图像的所有代码,
- void camera_UpdateImage()
- {
- pictureBox1.Image = camera.BitMap;
-
- }
- public static void CreateBitmap(out Bitmap bitmap, int width, int height, bool color)
- {
- bitmap = new Bitmap(width, height, GetFormat(color));
- if (bitmap.PixelFormat == PixelFormat.Format8bppIndexed)
- {
- ColorPalette colorPalette = bitmap.Palette;
- for (int i = 0; i < 256; i++)
- {
- colorPalette.Entries[i] = Color.FromArgb(i, i, i);
- }
- bitmap.Palette = colorPalette;
- }
- }
- public static void UpdateBitmap(Bitmap bitmap, byte[] buffer, int width, int height, bool color)
- {
- if (!IsCompatible( bitmap, width, height, color))
- {
- throw new Exception("Cannot update incompatible bitmap.");
- }
- BitmapData bmpData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
- /* Get the pointer to the bitmap's buffer. */
- IntPtr ptrBmp = bmpData.Scan0;
- /* Compute the width of a line of the image data. */
- int imageStride = GetStride(width, color);
- /* If the widths in bytes are equal, copy in one go. */
- if (imageStride == bmpData.Stride)
- {
- System.Runtime.InteropServices.Marshal.Copy(buffer, 0, ptrBmp, bmpData.Stride * bitmap.Height );
- }
- else /* The widths in bytes are not equal, copy line by line. This can happen if the image width is not divisible by four. */
- {
- for (int i = 0; i < bitmap.Height; ++i)
- {
- Marshal.Copy(buffer, i * imageStride, new IntPtr(ptrBmp.ToInt64() + i * bmpData.Stride), width);
- }
- }
- /* Unlock the bits. */
- bitmap.UnlockBits(bmpData);
- }
- }
复制代码 |