最近实现的功能需要把Winform传来的BitMap数组转为Texture2D,方法如下:
1. winform中从海康的NET_DVR_RigisterDrawFun画图回调返回的HDC中取出位图BitMap
[DllImport("Gdi32.dll")]
public static extern IntPtr GetCurrentObject(IntPtr hdc, uint type);
public byte[] GetBitMap(IntPtr intPtr)
{
IntPtr intPtr1 = GetCurrentObject(intPtr,7);
using (Bitmap bitmap = Image.FromHbitmap(intPtr1))
{
using (var ms = new System.IO.MemoryStream())
{
//bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
//return ms.ToArray();
bitmap.Save(ms, bitmap.RawFormat);
return ms.ToArray();
}
}
}
2. winform将BitMap位图的数组信息传递给unity
tempTexture.LoadImage(queueVS.Dequeue());
rawImage.texture = tempTexture;
queueVS是队列,将传递的位图信息存储到了此队列中