/// <summary>
/// 图像转haclon
/// </summary>
/// <param name="bitmap"></param>
/// <returns></returns>
public static HObject BitmapToHObject(Bitmap bitmap)
{
if (bitmap != null)
{
int width = bitmap.Width;
int height = bitmap.Height;
HObject result;
//HOperatorSet.GenEmptyObj(out hobject);
//Bitmap bitmap = (Bitmap)bitmap.Clone();
BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, bitmap.PixelFormat);
try
{
PixelFormat pixelFormat = bitmap.PixelFormat;
if (pixelFormat != PixelFormat.Format24bppRgb)
{
if (pixelFormat != PixelFormat.Format8bppIndexed)
{
if (pixelFormat != PixelFormat.Format32bppArgb && pixelFormat != PixelFormat.Format32bppRgb)
{
return null;
}
if (bitmapData.Stride != width * 4)
{
byte[] array = new byte[width * height * 4];
for (int i = 0; i < height; i++)
{
Marshal.Copy(new IntPtr(bitmapData.Scan0.ToInt64() + i * bitmapData.Stride), array, i * width * 4, width * 4);
}
unsafe
{
fixed (byte* pData = array)
{
HOperatorSet.GenImageInterleaved(out result, new IntPtr(pData), "bgrx", width, height, 0, "byte", 0, 0, 0, 0, -1, 0);
}
}
return result;
//Marshal.Copy(array, 0, bitmapData.Scan0, array.Length);//不改变原图 不用创建副本
}
HOperatorSet.GenImageInterleaved(out result, bitmapData.Scan0, "bgrx", width, height, 0, "byte", 0, 0, 0, 0, -1, 0);
}
else
{
if (bitmapData.Stride != width)
{
byte[] array2 = new byte[width * height];
for (int j = 0; j < height; j++)
{
Marshal.Copy(new IntPtr(bitmapData.Scan0.ToInt64() + j * bitmapData.Stride), array2, j * width, width);
}
unsafe
{
fixed (byte* pData = array2)
{
HOperatorSet.GenImage1(out result, "byte", width, height, new IntPtr(pData));
}
}
return result;
//Marshal.Copy(array2, 0, bitmapData.Scan0, width * height);
}
HOperatorSet.GenImage1(out result, "byte", width, height, bitmapData.Scan0);
}
}
else
{
if (bitmapData.Stride != width * 3)
{
byte[] array3 = new byte[width * height * 3];
for (int k = 0; k < height; k++)
{
Marshal.Copy(new IntPtr(bitmapData.Scan0.ToInt64() + k * bitmapData.Stride), array3, k * width * 3, width * 3);
}
unsafe
{
fixed (byte* pData = array3)
{
HOperatorSet.GenImageInterleaved(out result, new IntPtr(pData), "bgr", width, height, 0, "byte", 0, 0, 0, 0, -1, 0);
}
}
return result;
//Marshal.Copy(array3, 0, bitmapData.Scan0, array3.Length);
}
HOperatorSet.GenImageInterleaved(out result, bitmapData.Scan0, "bgr", width, height, 0, "byte", 0, 0, 0, 0, -1, 0);
}
}
catch
{
result = null;
}
finally
{
bitmap.UnlockBits(bitmapData);
}
return result;
}
return null;
}