我有以下代码:
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);
问题是bitmap.Palette
属性为null。为什么?我该如何解决这个问题?
修改
我将像素fortam设置为PixelFormats.Indexed8
,但我无法更改像素值以便从我的调色板设置特定索引。例如:
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();
为了将像素颜色设置为第二个palet颜色(在本例中为黄色),我设置为color_data
的值是多少?