I have a byte[]
that represents the raw data of an image. I would like to convert it to a BitmapImage
.
I tried several examples I found but I kept getting the following exception
"No imaging component suitable to complete this operation was found."
I think it is because my byte[]
does not actually represent an Image but only the raw bits.
so my question is as mentioned above is how to convert a byte[] of raw bits to a BitmapImage
.
When your byte array contains a bitmap's raw pixel data, you may create a BitmapSource
(which is the base class of BitmapImage
) by the static method BitmapSource.Create
.
However, you need to specify a few parameters of the bitmap. You must know in advance the width and height and also the PixelFormat
of the buffer.
byte[] buffer = ...;
var width = 100; // for example
var height = 100; // for example
var dpiX = 96d;
var dpiY = 96d;
var pixelFormat = PixelFormats.Pbgra32; // for example
var stride = (width * pixelFormat.BitsPerPixel + 7) / 8;
var bitmap = BitmapSource.Create(width, height, dpiX, dpiY,
pixelFormat, null, buffer, stride);