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

}

相关推荐
aini_lovee13 小时前
基于C#的三菱PLC串口通信实现方案
服务器·网络·c#
光泽雨14 小时前
c#MVVM中的消息通知机制
服务器·c#
江沉晚呤时14 小时前
C# 整型溢出处理机制:checked 与 unchecked 上下文解析
c#·.net
yngsqq15 小时前
Vlookup用法
c#
bitt TRES16 小时前
如何使用C#与SQL Server数据库进行交互
数据库·c#·交互
成都易yisdong17 小时前
C# 实现道路横断面自动生成与格式转换(最小二乘拟合 + 方向向量法)
windows·算法·c#·visual studio
田井中律.1 天前
知识图谱(关系抽取方法)【第十章】
人工智能·c#·知识图谱
周杰伦fans2 天前
C# CAD二次开发:RotatedDimension 文字边框设置完全指南
开发语言·c#
新缸中之脑2 天前
用Claude for Word审查法律合同
开发语言·c#·word
xuxie992 天前
N27 数据库UI
linux·c#