bmp转Hobject基本代码
public void Bitmap2HObjectBpp8(Bitmap SrcImage, out HObject image)
{
try
{
Point po = new Point(0, 0);
Size so = new Size(SrcImage.Width, SrcImage.Height);//template.Width, template.Height
Rectangle ro = new Rectangle(po, so);
Bitmap DstImg = new Bitmap(SrcImage.Width, SrcImage.Height, PixelFormat.Format8bppIndexed);
DstImg = SrcImage.Clone(ro, PixelFormat.Format8bppIndexed);
Rectangle rect = new Rectangle(0, 0, DstImg.Width, DstImg.Height);
BitmapData srcBmpData = DstImg.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
HOperatorSet.GenImage1(out image, "byte", DstImg.Width, DstImg.Height, srcBmpData.Scan0);
DstImg.UnlockBits(srcBmpData);
}
catch (Exception ex)
{
image = null;
}
}
bmp转Hobject进阶代码
public static HObject BitmapToHImage(Bitmap SrcImage)
{
HObject Hobj;
HOperatorSet.GenEmptyObj(out Hobj);
Point po = new Point(0, 0);
Size so = new Size(SrcImage.Width, SrcImage.Height);//template.Width, template.Height
Rectangle ro = new Rectangle(po, so);
Bitmap DstImage = new Bitmap(SrcImage.Width, SrcImage.Height, PixelFormat.Format8bppIndexed);
DstImage = SrcImage.Clone(ro, PixelFormat.Format8bppIndexed);
int width = DstImage.Width;
int height = DstImage.Height;
Rectangle rect = new Rectangle(0, 0, width, height);
System.Drawing.Imaging.BitmapData dstBmpData =
DstImage.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);//pImage.PixelFormat
int PixelSize = Bitmap.GetPixelFormatSize(dstBmpData.PixelFormat) / 8;
int stride = dstBmpData.Stride;
//重点在此
unsafe
{
int count = height * width;
byte[] data = new byte[count];
byte* bptr = (byte*)dstBmpData.Scan0;
fixed (byte* pData = data)
{
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++ )
{
data[i * width + j ] = bptr[i * stride + j];
}
HOperatorSet.GenImage1(out Hobj, "byte", width, height, new IntPtr(pData));
}
}
DstImage.UnlockBits(dstBmpData);
return Hobj;
}