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

}

相关推荐
咸鱼翻身小阿橙15 小时前
C# WinForms 控件学习项目
开发语言·学习·c#
JaydenAI15 小时前
[MAF预定义Agent中间件-03]FunctionInvocationDelegatingAgent:将AOP引入函数调用
ai·c#·agent·aop·maf
.NET修仙日记16 小时前
.NET 领域驱动设计:用户角色更新如何从应用服务落地到领域实体(代码拆解)
c#·.net·领域驱动设计·微软技术·角色设计
.NET修仙日记16 小时前
Scrutor:.NET 依赖注入自动化的优雅实现
c#·.net·.net core·微软技术·依赖注入·scrutor
xiaoshuaishuai816 小时前
C# Avalonia 依赖属性与WPF的区别
开发语言·c#·wpf
leo__5201 天前
C# 虚拟键盘(软键盘)实现
单片机·c#·计算机外设
周杰伦fans1 天前
AutoCAD C# 二次开发:如何精确监听工作空间切换事件
前端·c#
用户3721574261351 天前
如何使用 C# 自动调整 Excel 行高和列宽
c#
AI导出鸭PC端1 天前
智谱清言怎么生成word文档?AI导出鸭终结乱码烦恼
人工智能·ai·c#·word·豆包·ai导出鸭
xiaoshuaishuai81 天前
C# AvaloniaUI 中旋转
开发语言·c#