Given an array of bytes, where each byte represents a pixel value, you can create a bitmap in grayscale, as shown below. You need to specify the width and height of the bitmap, and this, of course, should match the size of the buffer.
byte[] buffer = ... // must be at least 10000 bytes long in this example var width = 100; // for example var height = 100; // for example var dpiX = 96d; var dpiY = 96d; var pixelFormat = PixelFormats.Gray8; // grayscale bitmap var bytesPerPixel = (pixelFormat.BitsPerPixel + 7) / 8; // == 1 in this example var stride = bytesPerPixel * width; // == width in this example var bitmap = BitmapSource.Create(width, height, dpiX, dpiY, pixelFormat, null, buffer, stride);
Each byte value can also be an index in the color palette, in which case you will need to specify PixelFormats.Indexed8
and, of course, pass the corresponding color palette.
source share