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

}

相关推荐
唐宋元明清21889 小时前
.NET Win32磁盘动态卷/跨区卷触发“函数不正确”问题排查
windows·c#·存储
hez20109 小时前
Satori GC:同时做到高吞吐、低延时和低内存占用
c#·.net·.net core·gc·clr
唐青枫1 天前
C#.NET Channel 深入解析:高性能异步生产者消费者模型实战
c#·.net
小峥降临2 天前
Rokid UXR 的手势追踪虚拟中更真实的手实战开发【含 工程源码 和 最终完成APK】
c#
晨星shine6 天前
GC、Dispose、Unmanaged Resource 和 Managed Resource
后端·c#
用户298698530146 天前
.NET 文档自动化:Spire.Doc 设置奇偶页页眉/页脚的最佳实践
后端·c#·.net
用户3667462526746 天前
接口文档汇总 - 2.设备状态管理
c#
用户3667462526746 天前
接口文档汇总 - 3.PLC通信管理
c#
Ray Liang7 天前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
Scout-leaf10 天前
WPF新手村教程(三)—— 路由事件
c#·wpf