PNG Files and Transparency

Bitmaps may or may not contain transparency. The transparent color is a RGB color for bitmaps that are not palettized (> 8 bits per pixel). For palettized bitmaps, the transparent color is an index to a color within the palette.

PNG supports transparent bitmaps that have one or more transparent colors. However, currently LEADTOOLS cannot handle more than one transparent color. Therefore, the first transparent color encountered by LEADTOOLS is used as the transparent color for the bitmap.

When loading PNG bitmaps that have transparency, LEADTOOLS sets BITMAPHANDLE.Flags.Transparency to 1 and sets BITMAPHANDLE.Transparency to the transparent color. Please note that if the loaded PNG bitmap is not palettized, then BITMAPHANDLE.Transparency is set to an RGB color. If the loaded PNG bitmap is palettized, BITMAPHANDLE.Transparency is set to 0x010000XX, where XX is the index of the transparent color.

When a bitmap that contains transparency (i.e. BITMAPHANDLE.Transparency is set) is saved as PNG, the transparency information is saved automatically. This is true for all bitmaps, regardless of their bits/pixel.

When loading 8-bits PNG files, you can use the L_GetPNGTRNS to get the current values for the transparency data used by LEADTOOLS. You can also use L_SetPNGTRNS to change the values for the transparency data before saving the 8-bits PNG files.

Please note that some developers consider the alpha channel to contain transparency information. LEADTOOLS does not make that assumption. Transparency is generally implemented in LEADTOOLS as explained above. However, L_FeatherAlphaBlendBitmap is a powerful function that can be used to implement transparency, using a mask saved as an alpha channel. If a bitmap contains a transparency mask in the alpha channel, call L_GetBitmapAlpha to get the alpha channel information back into a bitmap form. Once the transparency mask information is in a bitmap, it can be passed directly to the L_FeatherAlphaBlendBitmap function as the mask. For sample code that demonstrates how to do this, refer to Implementing Transparency. For more information on functions that utilize alpha channel information, refer to Alpha channel functions.

Some PNG files have an 8-bit palettized component (for the image) and 8-bit alpha information. For these files, FILEINFO.BitsPerPixel is 8 and FILEINFO.Flags has FILEINFO_HAS_ALPHA set. By default (when passing 0 for nBitsPerPixel when calling the load functions), the image is loaded as 8 bits (the alpha information is ignored). To get the alpha information from these files, you need to load the image as 32 or 64-bit. In this case, the alpha information will be loaded in the alpha channel. LEADTOOLS does not save these 8+8 files.

Help Version 23.0.2024.2.29
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

LEADTOOLS Raster Imaging C API Help
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.