【发布时间】:2017-05-23 15:23:11
【问题描述】:
我正在制作视频播放器。由于 cpu 问题,我想用 sharpdx sdk 绘制图像。但我有一个问题。我将帧作为字节数组。图像的像素格式为 BGRA32。我编写了以下代码来转换它。但它给了我错误“值不在预期范围内”。
我的代码是:
private Bitmap getBitmap(byte[] frame) {
return RenderTarget.CreateBitmap(new SizeU((uint)img_w, (uint)img_h), frame, 0, new BitmapProperties(new PixelFormat(DxgiFormat.B8G8R8A8_UNORM, AlphaMode.Ignore), 100, 100));
}
注意:img_w = 720,img_h = 576,帧数组长度 = 720 * 576 * 4
我首先尝试创建图像而不是将数据复制到图像。但它也不起作用。它将第一行复制到所有行。
左图是原图,右图是从字节数组复制而来。我使用以下代码创建此图像。
private Bitmap getBitmap(byte[] frame) {
var bmp = RenderTarget.CreateBitmap(new SizeU((uint)img_w, (uint)img_h), IntPtr.Zero, 0, new BitmapProperties(new PixelFormat(DxgiFormat.B8G8R8A8_UNORM, AlphaMode.Ignore), 100, 100));
bmp.CopyFromMemory(new RectU(0, 0, (uint)img_w, (uint)img_h), frame, 0);
return bmp;
}
【问题讨论】:
-
1) 您是否确认
frame包含您所期望的内容?也就是说,确保它不包含您所看到的内容(第一行重复)。 (打印出足够多的字节值来证明第二行与第一行不同。) 2) 尝试用随机字节填充它,看看是否再次得到重复的一行。 3) 另外,显示你的代码行 显示 新位图 - 那里可能有问题吗?
标签: c# .net bitmap sharpdx image-conversion