底层相机采集模块使用C++编写,需要传递给上层UI(C#)显示,即将非托管内存转换为Bitmap:
C#与C++接口图像表示:
/// <summary>
/// 底层图像表示
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct ImageParam
{
public int Width; //图像宽
public int Height; //图像高
public int Depth; //图像一行位深,8、16、24、32
public int Step; //图像一行byte数
public IntPtr ImagePtr; //图像数据指针
}
C#端转换代码:
[DllImport("kernel32.dll", EntryPoint = "CopyMemory", SetLastError = false)]
private static extern void CopyMemory(IntPtr dest, IntPtr src, uint count);
/// <summary>
/// ImageParam转Bitmap
/// </summary>
/// <param name="imageParam"></param>
/// <returns></returns>
public static Bitmap Convert(ImageParam imageParam)
{