Assuming you already have a 8-bits per pixel bitmap (a bitmap created with
PixelFormat.Format8bppIndexed
):
void ChangePalette(Bitmap image, Color[] colors)
{
ColorPalette palette = image.Palette;
for (int i = 0; i < 256; i++)
palette.Entries[i] = colors[i];
image.Palette = palette;
}
------------------------------------------
how can i change my bitmap image to PixelFormat.Format8bppIndexed?
Just create a new
Bitmap
:
Bitmap image = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
To fill this bitmap:
Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);
BitmapData bmpData = image.LockBits(rect, ImageLockMode.WriteOnly, image.PixelFormat);
byte[] values = new byte[bmpData.Stride * bmpData.Height];
for (int y = 0; y < bmpData.Height; y++)
for (int x = 0; x < bmpData.Width; x++)
values[y * bmpData.Stride + x] = 0;
System.Runtime.InteropServices.Marshal.Copy(values, 0, bmpData.Scan0, values.Length);
image.UnlockBits(bmpData);