0

帮助无法将字节数组转换为 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);
4

1 回答 1

1

CanvasBitmap.CreateFromSoftwareBitmap方法支持BitmapPixelFormat.Bgra8格式。对应 CanvasBitmap的格式BitmapPixelFormat.Bgra8DirectXPixelFormat.B8G8R8A8UIntNormalized。根据 Win2D 的Pixel 格式

如果有疑问,像素格式 B8G8R8A8UIntNormalized 和 CanvasAlphaMode Premultiplied 对于大多数用途来说都是很好的默认值。

您可能需要在创建时将其设置BitmapAlphaModePremultipliedwith 方法以使格式兼容。CreateCopyFromBuffer(IBuffer, BitmapPixelFormat, Int32, Int32, BitmapAlphaMode)SoftwareBitmap

SoftwareBitmap outputBitmap = SoftwareBitmap.CreateCopyFromBuffer(b.PixelBuffer, BitmapPixelFormat.Bgra8, b.PixelWidth, b.PixelHeight, BitmapAlphaMode.Premultiplied); 
于 2018-02-20T09:24:30.263 回答