帮助无法将字节数组转换为SoftwareBitmap。错误:
WriteableBitmap b = new WriteableBitmap(Weight*frame_size,Height*frame_size);
// WriteableBitmap uses BGRA format which is 4 bytes per pixel.
byte[] imageArray = new byte[b.PixelHeight * b.PixelWidth * 4];
for (int i = 0; i < imageArray.Length; i += 4)
{
imageArray[i] = 0; // Blue
imageArray[i + 1] = 0; // Green
imageArray[i + 2] = 255; // Red
imageArray[i + 3] = 0;
}
//Open a stream to copy the image contents to the WriteableBitmap's pixel buffer
using (Stream stream = b.PixelBuffer.AsStream())
{
await stream.WriteAsync(imageArray, 0, imageArray.Length);
}
SoftwareBitmap outputBitmap = SoftwareBitmap.CreateCopyFromBuffer(b.PixelBuffer,BitmapPixelFormat.Bgra8,b.PixelWidth,b.PixelHeight);
答案 0 :(得分:0)
CanvasBitmap.CreateFromSoftwareBitmap
方法支持BitmapPixelFormat.Bgra8
格式。 CanvasBitmap
的相应BitmapPixelFormat.Bgra8
格式为DirectXPixelFormat.B8G8R8A8UIntNormalized
。根据Win2D的Pixel formats:
如果有疑问,像素格式B8G8R8A8UIntNormalized和CanvasAlphaMode Premultiplied是大多数用途的良好默认值。
创建BitmapAlphaMode
时,您可能需要使用方法Premultiplied
将CreateCopyFromBuffer(IBuffer, BitmapPixelFormat, Int32, Int32, BitmapAlphaMode)
设置为SoftwareBitmap
以使格式兼容。
SoftwareBitmap outputBitmap = SoftwareBitmap.CreateCopyFromBuffer(b.PixelBuffer, BitmapPixelFormat.Bgra8, b.PixelWidth, b.PixelHeight, BitmapAlphaMode.Premultiplied);