I would like to draw something into an indexed bitmap. The problem is,
that the Graphics object may only be used for non-indexed images.
So I have to convert my indexed image into an nonindexed image in
order to be able to manipulate the image and then I have to reconvert
it to the old indexed format.
My problem is to reconvert the non-indexed image into an indexed
image. How is it possible?
I use following code:
private Bitmap ConvertToPixelFormat(Bitmap bitmap, PixelFormat
pixelFormat)
{
Rectangle rectangle = new Rectangle(0, 0, bitmap.Width,
bitmap.Height);
BitmapData bmpData = bitmap.LockBits(rectangle,
ImageLockMode.ReadWrite, bitmap.PixelFormat);
int byteCount = bmpData.Stride*bmpData.Height;
byte[] bytes = new byte[byteCount];
Marshal.Copy(bmpData.Scan0, bytes, 0, byteCount);
bitmap.UnlockBits(bmpData);
Bitmap bmpNew = new Bitmap(bitmap.Width, bitmap.Height,
pixelFormat);
BitmapData bmpDataNew = bmpNew.LockBits(rectangle,
ImageLockMode.ReadWrite, pixelFormat);
Marshal.Copy(bytes, 0, bmpDataNew.Scan0, bytes.Length);
bmpNew.UnlockBits(bmpDataNew);
return bmpNew;
}
The non-indexed image has got the pixel format Format32bppRgb and the
indexed image is of type Format8bppIndexed. I think, the problem is,
that the bytes array, which saves 4 bytes for each pixel is too big
for the indexed image taking only one byte for each pixel.
Any advice for me?
Thanks,
Norbert