簡單的將我們熟悉的Bitmap轉換成Halcon的HObject
下列例子為灰階或是32bit的影像格式轉換
若Bitmap為24bit建議先轉換成32bit(轉換方法)
原因在於24bit的每行padding byte不固定,傳換上會比較麻煩一點
轉換32bit後就會固定
下面程式碼會註解解說每一行的功能
public HObject BitmapToHoImage(Bitmap source)
{
if (source == null)
throw new ArgumentNullException("bmp不能為null");
HObject ho_Image;
HOperatorSet.GenEmptyObj(out ho_Image);
Rectangle rect = new Rectangle(0, 0, source.Width, source.Height);
if (source.PixelFormat == PixelFormat.Format24bppRgb)
{ source = source.ChangePixelFormatTo32Argb(true); }
unsafe
{
BitmapData bitmapData =
source.LockBits(rect,ImageLockMode.ReadOnly,source.PixelFormat);
IntPtr p = bitmapData.Scan0;
int ScanWidth;
switch (source.PixelFormat)
{
case PixelFormat.Format8bppIndexed:
ScanWidth = bitmapData.Stride;
HOperatorSet.GenImage1(out ho_Image, "byte", ScanWidth, source.Height, p);
HOperatorSet.CropPart(ho_Image, out ho_Image,
0, 0, source.Width, source.Height);
break;
case PixelFormat.Format32bppRgb:
case PixelFormat.Format32bppArgb:
HOperatorSet.GenImageInterleaved(out ho_Image, p,
"bgrx", source.Width, source.Height, -1, "byte", 0, 0, 0, 0, -1, 0);
break;
}
source.UnlockBits(bitmapData);
}
return ho_Image;
}
留言
張貼留言