Available in LEADTOOLS Imaging Pro, Vector, Document, and Medical Imaging toolkits. |
Implementing Transparency
You can implement transparency when doing any of the following:
Animation. When playing an animation, the playback process recognizes the transparent color and the transparency flag specified in the BITMAPHANDLE of each bitmap. The transparent color is loaded from and saved to GIF files. If you save animated files in other multi-page formats, you must maintain this BITMAPHANDLE information yourself. For more information, refer to Implementing Animation.
Paint effects. When painting a bitmap or other object, you can specify a transparent color. Keep in mind that you must use one of the special effects functions, such as LPaintEffect::PaintBitmap, and you must pass the color in a parameter. (For example, you could pass the transparent color that is specified in the BITMAPHANDLE of a GIF image.) For more information about paint effects, refer to Implementing Special Effects.
Region
processing. Defining a region in a bitmap makes it possible to
treat the area outside the region as transparent. The LPaint::PaintRgnDC
function paints only the region that is defined for a bitmap. The LBitmapBase::Combine
function combines only the area inside the region. You can define a region
in a number of ways, including specification of a color. For example,
if a transparent color is defined for a bitmap, you can create a region
that omits pixels of that color. For details, refer to Creating
a Bitmap Region.
Once a region is defined, you can create a mask from it, using the
LBitmapRgn::CreateMaskFromBitmapRgn
function, which you can save in the alpha channel of a 16- or 32-bit file.
Refer to the LBitmapBase::SetAlpha
function for an example of how to save a region in an alpha channel.
When specifying a transparent color, you can use the Windows PALETTEINDEX macro to refer to a particular palette index (where the COLORREF value is >= 0x01000000). Using a palette index for transparency means that one of the remaining colors in the palette can have the same RGB value as the transparent color, without being interpreted as transparent.
Setting Transparent Colors For Annotation Bitmap Objects (Document/Medical toolkits)
The annotation Stamp object (ANNOBJECT_STAMP, which includes the different Rubber Stamp tools) and Point object (ANNOBJECT_POINT) can be set to display bitmaps that use a transparent color. A transparent color is a color that is not painted when the image is painted. By default the transparent color is white (0x00FFFFFF). Use the following functions to work with transparent colors:
LAnnPoint Class:
LAnnPoint::GetTransparentColor
LAnnPoint::SetTransparentColor
LAnnStamp Class:
LAnnStamp::GetTransparentColor
LAnnStamp::SetTransparentColor
LAnnAutomation Class:
LAnnAutomation::GetTransparentColor
LAnnAutomation::SetTransparentColor
LAnnContainer Class:
LBitmapBase::FeatherAlphaBlend is a powerful function that can be used to implement transparency, using a mask. In a many applications, a transparency mask is saved as an alpha channel. If a bitmap contains a transparency mask in the alpha channel, call CreateAlphaBitmap 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 LBitmapBase::FeatherAlphaBlend function as the mask. The following code demonstrates how to do this:
void TestFunction(HWND hWnd)
{
LBitmapWindow Image;
LBitmapBase MaskBitmap,AlphaBitmap,DstBitmap;
int nRet;
Image.SetWndHandle(hWnd);
Image.Load(TEXT("c:\\parrots.jpg"));
/* Specify a rectangle to define the region */
RECT Rect={10,10,50,200};
LBitmapRgn Region(&Image);
// check if the bitmap region is valid or not
if (Region.IsValid() ==
FALSE)
Region.SetBitmap(&Image)
;
/* Create an elliptical region in the AlphaBitmap */
Region.SetRgnEllipse(&Rect);
/* Create a mask bitmap from the region */
nRet = Region.CreateMaskFromBitmapRgn(&MaskBitmap,
sizeof(BITMAPHANDLE)) ;
/* Update the alpha channel in the main bitmap */
Image.SetAlpha(MaskBitmap);
/* Save the bitmap at 16 bits per pixel to keep the alpha channel */
Image.Save(TEXT("c:\\Test.Tif"),
FILE_TIF, 16, 0,1, 0);
/* Load the bitmap that we just saved and get its alpha channel */
Image.Load(TEXT("c:\\Test.Tif"));
AlphaBitmap.CreateAlphaBitmap(Image);
DstBitmap.Create(AlphaBitmap.GetWidth(), AlphaBitmap.GetHeight(), 24, ORDER_BGR,
NULL, TOP_LEFT, TYPE_CONV);
nRet = DstBitmap.FeatherAlphaBlend(0,
0, AlphaBitmap.GetWidth(),
AlphaBitmap.GetHeight(),
&Image, 0, 0, &MaskBitmap);
Image.SetHandle(DstBitmap.GetHandle());
}