0

Help cant convert byte array to SoftwareBitmap. Error: 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);
0

1 Answer 1

1

CanvasBitmap.CreateFromSoftwareBitmap method supports BitmapPixelFormat.Bgra8 format. The corresponding CanvasBitmap's Format for BitmapPixelFormat.Bgra8 is DirectXPixelFormat.B8G8R8A8UIntNormalized. According to the Pixel formats of Win2D:

If in doubt, pixel format B8G8R8A8UIntNormalized and CanvasAlphaMode Premultiplied are good defaults for most purposes.

You may need to set the BitmapAlphaMode to Premultiplied with method CreateCopyFromBuffer(IBuffer, BitmapPixelFormat, Int32, Int32, BitmapAlphaMode) when create the SoftwareBitmap to make the format compatible.

SoftwareBitmap outputBitmap = SoftwareBitmap.CreateCopyFromBuffer(b.PixelBuffer, BitmapPixelFormat.Bgra8, b.PixelWidth, b.PixelHeight, BitmapAlphaMode.Premultiplied); 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.