Handling Palette Changes
Whenever you paint an image to a device of 256 colors or less, the current system palette is used. This topic describes how to ensure that the current system palette is the one you want to use.
First, consider that LEADTOOLS gives you the following two choices for painting to a device of 256 colors or less:
- A fixed palette (LEAD's fixed palette or the Netscape palette). The fixed palette is the same for any image. It is good to use when more than one image is displayed at the same time and you want all of the images to look good. A fixed palette is always used when painting a high-color or true-color image. The default is the LEAD fixed palette, but you can specify the Netscape palette for 256-color devices.
- The image's own palette. This is the best available representation of the image's colors. It may be an optimized palette or a fixed palette, depending on how the image was created.
Generally, the following order of execution ensures that the current system palette is the one you want to use:
Set the display mode to turn on or turn off the fixed palette when painting all images. The following code turns on the LEAD fixed palette:
PaintDisplayMode = FixedPalette;
The following code turns off the LEAD fixed palette:
PaintDisplayMode = None;
Create the paint palette. The RasterImagePainter.GetPaintPalette method returns a palette that you want to use. If the fixed palette is turned on, this is always the fixed palette. Otherwise it is the best available palette for the specified image. The following code shows the variable declarations and the method call. (The scope of the variables is application dependent; they are shown all together here for simplicity.)
string srcFileName = @"d:\lt16\Images\eye.gif";
RasterImage srcImage = codecs.Load(srcFileName);
Graphics g = this.CreateGraphics();
System.Drawing.Imaging.ColorPalette palPaint = srcImage.GetPaintPalette(g);
Call one of the LEADTOOLS painting methods. The following example uses Rectangle variables that are defined elsewhere, as described in the comments:
Rectangle dstRect = new Rectangle(0, 0, srcImage.Width, srcImage.Height);
RasterPaintProperties props = RasterPaintProperties.Default;
srcImage.Paint(g, Rectangle.Empty, dstRect, props);
Please note that if you are painting a 16-bit grayscale image to a 24-bit device, you must call RasterImagePainter.GetPaintPalette before you call RasterImagePainter.Paint.