Problem with Bitmap.Save after Palette Changed

_SBradley_

Active member
Joined
Jul 2, 2003
Messages
28
Location
Cardiff, Wales, UK
If I run the following code on a 16-colour bitmap, the changes to the palette are not included in the saved image. However, the image displayed in the form is as I expected (black & white). Am I doing something wrong, or is this a bug?

[CS]
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

class Test
{
static void Main(string[] args)
{
Bitmap bmp = new Bitmap(args[0]);
ColorPalette pal = bmp.Palette;

for (int i = 0; i < pal.Entries.Length; ++i)
pal.Entries = i == 0 ? Color.Black : Color.White;

bmp.Palette = pal;
bmp.Save("new.bmp");
new ImgForm(bmp).ShowDialog();
}
}

class ImgForm : Form
{
public ImgForm(Bitmap bmp)
{
m_Bitmap = bmp;
}

protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawImage(m_Bitmap, ClientRectangle);
}

private Bitmap m_Bitmap;
}
[/CS]
 
Since the constructor youre using is only specifying the filename as a string, the bitmap class is defaulting to (I believe) 32 bits. Since 32 bit has no palette, its likely being ignored. Youll most need to include a PixelFormat arg as well.
 
Back
Top