EDN Admin
Well-known member
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)
<div style="color:black; background-color:white
<pre> ColorPalette pal;
pal = bmp.Palette;
<span style="color:blue for (<span style="color:blue int i = 16; i < 40; i++)
{
pal.Entries = Color.FromArgb(i, i, i);
}
bmp.Palette = pal;
[/code]
2. Load the bmp using WPF new classes:
<div style="color:black; background-color:white
<pre> <span style="color:blue public <span style="color:blue static Bitmap LoadPNG(<span style="color:blue string file)
{
<span style="color:green //using (Bitmap PNG = new Bitmap(1, 1, PixelFormat.Format8bppIndexed))
<span style="color:green // PNG.Save("Test.png", ImageFormat.Png);
PngBitmapDecoder pngDec = <span style="color:blue new PngBitmapDecoder(<span style="color:blue new Uri(file),
BitmapCreateOptions.PreservePixelFormat,
BitmapCacheOption.OnDemand);
BmpBitmapEncoder bmpEnc = <span style="color:blue new BmpBitmapEncoder();
bmpEnc.Frames.Add(pngDec.Frames[0]);
Bitmap bmp = <span style="color:blue null;
<span style="color:blue using (MemoryStream stream = <span style="color:blue new MemoryStream())
{
bmpEnc.Save(stream);
bmp = <span style="color:blue new Bitmap(stream);
}
<span style="color:blue return bmp;
}
[/code]
<br/>
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..
<br/>
<br/>
View the full article
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)
<div style="color:black; background-color:white
<pre> ColorPalette pal;
pal = bmp.Palette;
<span style="color:blue for (<span style="color:blue int i = 16; i < 40; i++)
{
pal.Entries = Color.FromArgb(i, i, i);
}
bmp.Palette = pal;
[/code]
2. Load the bmp using WPF new classes:
<div style="color:black; background-color:white
<pre> <span style="color:blue public <span style="color:blue static Bitmap LoadPNG(<span style="color:blue string file)
{
<span style="color:green //using (Bitmap PNG = new Bitmap(1, 1, PixelFormat.Format8bppIndexed))
<span style="color:green // PNG.Save("Test.png", ImageFormat.Png);
PngBitmapDecoder pngDec = <span style="color:blue new PngBitmapDecoder(<span style="color:blue new Uri(file),
BitmapCreateOptions.PreservePixelFormat,
BitmapCacheOption.OnDemand);
BmpBitmapEncoder bmpEnc = <span style="color:blue new BmpBitmapEncoder();
bmpEnc.Frames.Add(pngDec.Frames[0]);
Bitmap bmp = <span style="color:blue null;
<span style="color:blue using (MemoryStream stream = <span style="color:blue new MemoryStream())
{
bmpEnc.Save(stream);
bmp = <span style="color:blue new Bitmap(stream);
}
<span style="color:blue return bmp;
}
[/code]
<br/>
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..
<br/>
<br/>
View the full article