Question about indexed and non-indexed bitmap!  
Author Message
xhy_China





PostPosted: Visual C# General, Question about indexed and non-indexed bitmap! Top

Hi,I have three Questions:

1. How can I know whether a bitmap is a indexed one or non-indexed one

2. How can I convert non-indexed bitmap to indexed

3. How can I convert indexed bitmap to non-indexed



Visual C#3  
 
 
nobugz





PostPosted: Visual C# General, Question about indexed and non-indexed bitmap! Top

1: Use the Image.PixelFormat property

2: GDI+ doesn't support this. There is however a trick available that you can play with the GIF image encoder. The result isn't always very good, the encoder uses dithering if necessary. You can only generate the 8bpp indexed format. Here's the code:

public static Image Image2Indexed8bpp(Image img) {
System.IO.MemoryStream buffer = new System.IO.MemoryStream();
img.Save(buffer, System.Drawing.Imaging.ImageFormat.Gif);
buffer.Seek(0, System.IO.SeekOrigin.Begin);
return Bitmap.FromStream(buffer);
}

3: The Graphics class can do this for you. Here's the code:

public static Image Indexed2Image(Image img, System.Drawing.Imaging.PixelFormat fmt) {
Image bmp = new Bitmap(img.Width, img.Height, fmt);
Graphics gr = Graphics.FromImage(bmp);
gr.DrawImage(img, 0, 0);
gr.Dispose();
return bmp;
}