Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to convert from unindexed to indexed image?

11 views
Skip to first unread message

Norbert Pürringer

unread,
Feb 14, 2008, 10:42:27 AM2/14/08
to
Hello there,

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

0 new messages