图像的处理 图像转haclon

/// <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;

}

相关推荐
bugcome_com5 小时前
C# 运算符详解:类型、实例及优先级
c#
C#程序员一枚7 小时前
Web Service 和 Web API
c#
MaoziShan1 天前
CMU Subword Modeling | 09 Lexemes, or What Dictionaries Know about Morphology
开发语言·人工智能·机器学习·语言模型·自然语言处理·c#
游乐码1 天前
c#选择排序
c#·排序算法
listhi5201 天前
基于C#实现动态人脸检测
开发语言·c#
rabbitlzx1 天前
《Async in C# 5.0》第十四章 深入探讨编译器对于async的转换
java·开发语言·c#·异步·asynchronous
智商偏低1 天前
unity 如何渲染大场景
c#
光泽雨1 天前
单例模式代码理解
开发语言·c#