图像的处理 图像转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;

}

相关推荐
csdn_aspnet10 小时前
C# 求n边凸多边形的对角线数量(Find number of diagonals in n sided convex polygon)
开发语言·算法·c#
武藤一雄14 小时前
C# 设计模式大全(第一弹|7种)
microsoft·设计模式·微软·c#·.net·.netcore
格林威16 小时前
Baumer相机锂电池极片裁切毛刺检测:防止内部短路的 5 个核心方法,附 OpenCV+Halcon 实战代码!
开发语言·人工智能·数码相机·opencv·计算机视觉·c#·视觉检测
向上的车轮16 小时前
熟悉C#如何转TypeScript——SDK与包引用
开发语言·typescript·c#
CSharp精选营18 小时前
Dispose 不释放?C# 资源泄漏的 3 种隐蔽场景排查
c#·资源泄漏
unicrom_深圳市由你创科技19 小时前
LabVIEW和C#在工业控制中的应用差异是什么?
fpga开发·c#·labview
唐青枫20 小时前
C#.NET Consul + Steeltoe 深入解析:服务注册发现、健康检查与微服务接入
c#·.net
DowneyJoy20 小时前
【Unity3D补充知识点】常用数据结构分析-集合(List<T>)
数据结构·unity·c#·list
格林威21 小时前
Baumer相机铝型材表面划伤长度测量:实现损伤量化评估的 5 个关键技术,附 OpenCV+Halcon 实战代码!
开发语言·人工智能·数码相机·opencv·计算机视觉·c#·工业相机
DowneyJoy21 小时前
【Unity3D补充知识点】常用数据结构分析-数组(Array)
数据结构·unity·c#