不可支持的位图格式。字节数组到SoftwareBitmap(UWP C#)

时间:2018-02-19 13:49:55

标签: c# uwp win2d

帮助无法将字节数组转换为SoftwareBitmap。错误: enter image description here

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);

1 个答案:

答案 0 :(得分:0)

CanvasBitmap.CreateFromSoftwareBitmap方法支持BitmapPixelFormat.Bgra8格式。 CanvasBitmap的相应BitmapPixelFormat.Bgra8格式为DirectXPixelFormat.B8G8R8A8UIntNormalized。根据Win2D的Pixel formats

  

如果有疑问,像素格式B8G8R8A8UIntNormalized和CanvasAlphaMode Premultiplied是大多数用途的良好默认值。

创建BitmapAlphaMode时,您可能需要使用方法PremultipliedCreateCopyFromBuffer(IBuffer, BitmapPixelFormat, Int32, Int32, BitmapAlphaMode)设置为SoftwareBitmap以使格式兼容。

SoftwareBitmap outputBitmap = SoftwareBitmap.CreateCopyFromBuffer(b.PixelBuffer, BitmapPixelFormat.Bgra8, b.PixelWidth, b.PixelHeight, BitmapAlphaMode.Premultiplied);