Google 網路論壇不再支援新的 Usenet 貼文或訂閱項目,但過往內容仍可供查看。

How to convert from unindexed to indexed image?

瀏覽次數:11 次
跳到第一則未讀訊息

Norbert Pürringer

未讀,
2008年2月14日 上午10:42:272008/2/14
收件者:
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 則新訊息