Transparency Class
Description
The Transparency class is a class used to add transparency to a System.Drawing.Bitmap object. This is done by assigning a custom System.Drawing.Imaging.ColorPalette to the Bitmap's Palette property. The MakeTransparent() function of the Transparency class can be used not only to add transparency to an image, but to insure that the colors used when creating the image are the colors used when saving the image. This can often be necessary because when saving using the System.Drawing.Imaging.ImageFormat.Gif format, a predefined palette is used, causing a slightly different color or even a checkerboard pattern of multiple colors to replace the colors you used when creating your image.
Example
The following three images were generated by the same code. The first is saved without modification, the second customizes the palette but does not make it transparent, and the third modifies the palette and makes the image transparent. The statements used to save these images is listed below.

Unmodified Image Modified Palette Transparent Image
Unmodified
Image
Modified
Palette
Transparent
Image
Properties & Methods
MakeTransparent(ByVal oldbmp As Bitmap, ByVal transparentcolor As Color) As BitmapUsed to make a custom palette that contains the colors used in the original Bitmap rather than the predefined palette, and optionally make one color transparent. The number of colors is limited to 256 (one of which must be the color that will be made transparent if adding transparency).
  • oldbmp - The Bitmap to be modified
  • transparentcolor - The Color that you would like to make transparent. If you would like to have the palette corrected but do not need or want any colors made transparent, use the value Color.Empty.


ColorCount(ByVal bmp As Bitmap) As IntegerUsed to determine how many colors are used in the Bitmap. If you have used any methods or techniques when creating your Bitmap that dynamically determine colors, or use multiple colors, this can help you determine whether or not it can be used with the MakeTransparent function.
  • bmp - The Bitmap of which to count the Colors


InPalette(ByVal palette As ColorPalette, ByVal maxindex As Byte, ByVal colortofind As Color) As IntegerUsed to return the index of the specified Color in the palette. The possible return values are 0 through 255 or -1 if the color does not exist in the palette. This function is primarily used internally, but may be accessed as a Shared function as well.
  • palette - The ColorPalette to be searched
  • maxindex - The maximum index to search. If you would like to search the entire palette, you may enter a value of 255
  • colortofind - The color to return the index of
Source Code
Transparency.vb:

Remarks
The Transparency class is intended to be used with images that will be saved as PixelFormat.Gif. If you are unsure of the number of colors used in your image, be sure to use the ColorCount function. If your image has more than 256 colors, you may receive an error, and be sure to remember that one of those 256 colors must be the same as the transparentcolor in order to add transparency to your image.

I would also like to give a special thank you to Yosi K. Karl for helping me find and fix a bug in the Transparency class.