GDI+ or .NET bug - 8bpp PNG loaded as 32bpp

  • Thread starter Thread starter FreeCoderr
  • Start date Start date
F

FreeCoderr

Guest
There is a bug in GDI (or .NET).

Steps to Reproduce:

1. Create a 8bpp Bitmap

Bitmap bmp = new Bitmap(200, 200, PixelFormat.Format8bppIndexed);

2. Save it to a file

bmp.Save("pngFile.png", ImageFormat.Png);

3. Load it as a new bitmap

Bitmap bmpFromPNG = new Bitmap(filenamePNG);

The PixelFormat will be 32bpp! It should be 8bpp!!! If you do the same with a BMP the format will be the correct (8bpp).

Solutions that I found:

1. Change the bmp palette (at least from rang 16 to 40)


ColorPalette pal;
pal = bmp.Palette;
for (int i = 16; i < 40; i++)
{
pal.Entries = Color.FromArgb(i, i, i);
}
bmp.Palette = pal;




2. Load the bmp using WPF new classes:


public static Bitmap LoadPNG(string file)
{
//using (Bitmap PNG = new Bitmap(1, 1, PixelFormat.Format8bppIndexed))
// PNG.Save("Test.png", ImageFormat.Png);
PngBitmapDecoder pngDec = new PngBitmapDecoder(new Uri(file),
BitmapCreateOptions.PreservePixelFormat,
BitmapCacheOption.OnDemand);
BmpBitmapEncoder bmpEnc = new BmpBitmapEncoder();
bmpEnc.Frames.Add(pngDec.Frames[0]);
Bitmap bmp = null;
using (MemoryStream stream = new MemoryStream())
{
bmpEnc.Save(stream);
bmp = new Bitmap(stream);
}
return bmp;
}



What do you think about that, is it GDI+ bug?

Emgucv does have a similar bug, if the palette is not in grayscale, a 8bpp image will be loaded incorrectly.. :(

Continue reading...
 
Back
Top