問題描述
我一直在編寫 PCX 解碼器,到目前為止,PCX 圖像本身解析得很好,但我不知道如何設置位圖的調色板.
I have been writing a PCX decoder and, so far, the PCX image itself parses fine, but I can't work out how to set the palette of a bitmap.
我已經創建了一個像這樣的位圖:
I have created a bitmap like so:
Bitmap bmp = new Bitmap(width,
height,
stride2,
System.Drawing.Imaging.PixelFormat.Format8bppIndexed,
pixels);
但我似乎無法使用以下方法設置調色板:
But I can't seem to set the palette using the following method:
for (int i = 0; i < 256; i += 3)
{
Color b = new Color();
b = Color.FromArgb(palette[i], palette[i + 1], palette[i + 2]);
bmp.Palette.Entries.SetValue(b, i);
}
在這個例子中,我已經閱讀了 pcx 文件調色板中的每個字節并將它們存儲在調色板 [] 中.從那里,我用它來設置位圖調色板中的條目.如何設置顏色?
In this example, I have read through each byte in the palette of the pcx file and stored them in palette[]. from there, I have used this to set the entries in the palette of the bitmap. How do I set the colours?
推薦答案
這也讓我感到困惑.似乎 bitmap.Palette 返回位圖調色板的克隆.一旦你修改了你的副本,你需要使用 bitmap.Palette = Palette
重置位圖的調色板,例如
This had me confused too. It seems bitmap.Palette returns a clone of the bitmap's palette. Once you've modified your copy, you need to reset the bitmap's pallete by using bitmap.Palette = palette
, e.g.
ColorPalette palette = bitmap.Palette;
Color entries = palette.Entries;
....
entries[i] = new Color(...);
....
bitmap.Palette = palette; // The crucial statement
參見 http://www.charlespetzold.com/pwcs/PaletteChange.html
這篇關于無法在位圖中設置調色板的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!