Send comments on this topic. | Back to Introduction - All Topics | Help Version 15.12.21
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:

1.

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;

2.

Create the paint palette. The RasterImage.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:\lt15\Images\eye.gif";
RasterImage srcImage = codecs.Load(srcFileName);

Graphics g = this.CreateGraphics();
System.Drawing.Imaging.ColorPalette palPaint = srcImage.GetPaintPalette(g);

3.

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 RasterImage.GetPaintPalette before you call RasterImage.Paint.