WritableBitmap: Memory allocation mismatch - How to solve?

37 Views Asked by At

In my program I try to read a color palette image's (FormatConvertedBitmap with format Indexed4) pixels using a WritableBitmap.

The dimensions of the resulting image are 20 * 27 pixels.

The color palette is 16 colors, so each color takes a nibble and each byte carries two pixels.

However, in memory, WPF adds an additional DWORD to the end of each pixel row:

WPF WritableBitmap memory allocation

I'm not able to address particular pixels with this mismatch.


What BitmapSource property do I need to examine to be able to compute the correct stride of this image?

1

There are 1 best solutions below

0
Clemens On BEST ANSWER

A WPF BitmapSource may use whatever stride it prefers, provided that it can hold the values of all pixels in a row. In particular, it may adjust the stride for whatever kind of optimized memory access.

The size of the underlying buffer is calculated by multiplying the stride with the number of rows:

WritableBitmap bitmap = ...
var bufferSize = bitmap.BackBufferStride * bitmap.PixelHeight;