C# 为什么我的位图调色板为空?
我有以下代码:C# 为什么我的位图调色板为空?,c#,wpf,bitmap,palette,writablebitmap,C#,Wpf,Bitmap,Palette,Writablebitmap,我有以下代码: 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,
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);
要将像素颜色设置为第二种调色板颜色(即本例中的黄色),我必须将什么值设置为
color\u data
。Bgr32格式不使用调色板,因为每个像素的颜色值直接编码为四个字节的元组,一个用于蓝色,一个用于绿色,一个用于红色,还有一个未使用。有关所有可能格式的详细信息,请参阅。只有索引*
格式使用调色板。@克莱门斯,我应该如何更改位图的像素值以选择合适的调色板索引?请写下你的答案。注意你的意思。Bgr32位图中没有调色板索引。要设置像素值,请使用WriteableBitmap的WritePixels重载之一。@Clemens请参阅我的编辑。问题中的代码没有将其设置为Indexed8,而是将其设置为Bgr32,因此没有调色板。“Indexed8”表示每个像素为1字节,从调色板中查找颜色值,但即使您的代码似乎也表明您使用的是32位像素,pBackBuffer+=column*4代码>。请仔细检查您是否实际设置了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();