通过像素数据生成VisionPro的CogImage8Grey图像

话不多说,直接上码

// 读取BitMap图像
System.Drawing.Bitmap curBitmap = new Bitmap("00.bmp");

// 定义处理区域
Rectangle rect = new Rectangle(0, 0, curBitmap.Width, curBitmap.Height);

// 获取像素数据
System.Drawing.Imaging.BitmapData bmpData = curBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, curBitmap.PixelFormat);

// 获取像素数据指针
IntPtr IntPtrPixelData = bmpData.Scan0;

// 定义Buffer
byte[] PixelDataBuffer = new byte[curBitmap.Width * curBitmap.Height];

// 拷贝Bitmap的像素数据的到Buffer
System.Runtime.InteropServices.Marshal.Copy(IntPtrPixelData, PixelDataBuffer, 0, PixelDataBuffer.Length);

// 创建CogImage8Grey
Cognex.VisionPro.CogImage8Grey cogImage8Grey = new Cognex.VisionPro.CogImage8Grey(curBitmap.Width, curBitmap.Height);

// 获取CogImage8Grey的像素数据指针
IntPtr Image8GreyIntPtr = cogImage8Grey.Get8GreyPixelMemory(Cognex.VisionPro.CogImageDataModeConstants.Read, 0, 0, cogImage8Grey.Width, cogImage8Grey.Height).Scan0; 

// 拷贝Buffer数据到CogImage8Grey的像素数据区
System.Runtime.InteropServices.Marshal.Copy(PixelDataBuffer, 0, Image8GreyIntPtr, PixelDataBuffer.Length);

// 显示图像
cogDisplay1.Image = cogImage8Grey;
cogDisplay1.Fit();

 

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