Microsoft KB Archive/318343

From BetaArchive Wiki

Article ID: 318343

Article Last Modified on 2/12/2007



APPLIES TO

  • Microsoft GDI+ 1.0
  • Microsoft .NET Framework Software Development Kit 1.0
  • Microsoft Windows XP Professional
  • Microsoft Windows XP Professional for Itanium-based systems



This article was previously published under Q318343

This article references the following .NET Framework Class Library namespaces:

  • System.Drawing


SUMMARY

A GIF image file that is created by the GDI+ classes or by the System.Drawing classes is always saved using an 8 bits-per-pixel (bpp) image. This occurs because the encoder in the GIF codec that ships with GDI+ version 1.0 processes only 8-bpp Bitmaps or Images.

MORE INFORMATION

Although GDI+ 1.0 and the .NET Framework can work with Bitmap image objects in several different pixel formats, the GIF codec that is used with the Bitmap.Save() method can encode only the 8 bits-per-pixel format.

The GIF encoder is capable of processing both 8-bpp and 32-bpp images. When the GIF encoder receives a request to save a 32-bpp image, the GIF encloder converts the image to 8 bpp by half-toning the image. If the bitmap's pixel format is not the GIF encoder-supported 32-bpp or 8-bpp format, the Save() method converts the image to 32 bpp so that the GIF encoder can convert the image to the 8-bpp format before encoding. Thus, the GIF encoder always processes an 8-bpp format.

This may be unexpected in these ways:

  • The conversion that the GIF encoder uses to create an 8-bpp image from a 32-bpp image automatically sets the palette to the default halftone palette. As a result, the GIF encoder ignores any palette that may have been set for the image.
  • Other indexed pixel formats (for example, 1 bpp or 4 bpp) always result in a 256-color GIF file because the Bitmap.Save() method promotes the format to 32 bpp before giving the file to the GIF encoder, which then color reduces to a 256-entry color table.

When the Bitmap object's format is 8 bpp and the object is saved with the GIF codec, the codec encodes the bitmap's palette entries into the smallest GIF color table such that the color table contains all of the palette entries and does not exceed 256 (per the GIF specification).

GIF Files with Less Than 256 colors

To create GIF files containing less than 256 colors, use an 8-bpp Bitmap object, but reduce the size of the palette and set the pixel indexes appropriately.

To save a monochrome GIF image file, follow these steps:

  1. Set the size of the Bitmap object's palette to two colors.
  2. Initialize the palette with two colors, presumably black and white.
  3. Create the Bitmap's definition by setting each 8-bpp color index to a byte value of zero (0) or one (1) to reference either of the palette's two colors
  4. Save the bitmap using the GIF codec.

To save a 16-color GIF image file, follow these steps:

  1. Set the size of the file share Bitmap object's palette to 16 entries.
  2. Initialize the palette with 16 colors for entries zero (0) through 15.
  3. Create the Bitmap's definition by setting each 8-bpp color index to a byte value between zero (0) and 15.
  4. Save the bitmap using the GIF codec.

In each case, the GDI+ Bitmap object is an 8-bpp format image, but the Bitmap definition uses only a subset of the 8-bpp color palette (which becomes the GIF color table). The GIF codec sets the file's global color table to a size sufficiently large to contain the entries of the 8-bpp Bitmap's palette, and then the GIF codec encodes the significant bits of the image's color indexes for that color table.

REFERENCES

For additional information about saving a GIF with a new color table, click the article number below to view the article in the Microsoft Knowledge Base:

315780 HOWTO: Save a GIF File with a New Color Table By Using GDIplus


Third-party License

Microsoft has obtained a license from Unisys to use the GIF file format and other LZW technologies that are covered by the Unisys-owned U.S. and foreign patents in a number of Microsoft products. However, this license does not extend to third-party developers who use Microsoft development products or toolkits to develop applications. As a third-party developer, you need to determine whether you need to obtain a license from Unisys to use the GIF format or the LZW technologies.

For additional information about LZW licenses and GIF, click the article number below to view the article in the Microsoft Knowledge Base:

193543 INFO: Unisys GIF and LZW Technology License Information


GIF Defined

Graphics Interchange Format (GIF) is a stream-capable image file format that was created by CompuServe.


Additional query words: System.Drawing Bitmap::Save Image::Save PixelFormat1bppIndexed PixelFormat4bppIndexed PixelFormat8bppIndexed PixelFormat16bppARGB1555 PixelFormat16bppGrayScale PixelFormat16bppRGB555 PixelFormat16bppRGB565 PixelFormat24bppRGB PixelFormat32bppARGB PixelFormat32bppPARGB PixelFormat32bppRGB PixelFormat48bppRGB PixelFormat64bppARGB PixelFormat64bppPARGB

Keywords: kbdswgdi2003swept kbcolor kbfaq kbgdipimaging kbinfo KB318343