I have the following code:
List<Color> colors = new List<Color>();
colors.Add(Colors.Red);
colors.Add(Colors.Yellow);
BitmapPalette palette = new BitmapPalette(colors);
var bitmap = new WriteableBitmap( (int)width,
(int)height,
96,
96,
PixelFormats.Bgr32,
palette);
The proble is that bitmap.Palette
property is null. Why? How can I fix this problem?
Edit:
I set the pixel fortam as PixelFormats.Indexed8
, but I am unable to change the pixel value in order to set a specific index form my palette. For example:
int column = ...;
int row = ...;
// Reserve the back buffer for updates.
bitmap.Lock();
unsafe
{
// Get a pointer to the back buffer.
int pBackBuffer = (int)bitmap.BackBuffer;
// Find the address of the pixel to draw.
pBackBuffer += row * bitmap.BackBufferStride;
pBackBuffer += column * 4;
// Compute the pixel's color.
int color_data = ???;
// Assign the color data to the pixel.
*((int*)pBackBuffer) = color_data;
}
// Specify the area of the bitmap that changed.
bitmap.AddDirtyRect(new Int32Rect(column, row, 1, 1));
// Release the back buffer and make it available for display.
bitmap.Unlock();
What value have I to set to color_data
in order to set the pixel color to the second palet color (that is, yellow in this case)?