Implementing Transparency

You can implement transparency when doing any of the following:

image\sqrblit.gif Animation. When playing an animation, the playback process recognizes the transparent color specified in the BitmapTransparentColor property if the BitmapEnableTransparency property is set to TRUE. The transparent color is loaded from and saved to GIF files. If you save animated files in other multi-page formats, you must maintain the color yourself. For more information, refer to Playing an Animation.

image\sqrblit.gif Paint effects. When painting a bitmap or other object, you can specify a transparent color in the BitmapTransparentColor property and set the BitmapEnableTransparency property. For more information about painting images, refer to Displaying an Image.

image\sqrblit.gif Displaying an image with transparent color. When painting a bitmap, you can specify a transparent color in the BitmapTransparentColor property and set the BitmapEnableTransparency property to TRUE. LEAD will then paint the bitmap without the color set in the BitmapTransparentColor property.

Note: When loading an image file of any supported file format, LEADTOOLS ActiveX control will load the transparent color information into the BitmapTransparentColor property. You must assign the BitmapTransparentColor property and set the BitmapEnableTransparency property to TRUE for proper display.

image\sqrblit.gif Region processing. Defining a region in a bitmap makes it possible to treat the area outside the region as transparent. You can set the PaintRgnOnly property to TRUE to paint only the region that is defined for a bitmap. When combining images with the Combine method, if a region is defined for the target bitmap, the combine applies only to the region. (Any region defined in the source bitmap is ignored.). 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 Combining Images.

To combine image data from the source bitmap (the slave) into the destination bitmap (master) using a perspective warp, use the CombineBitmapWarp method. If you want to specify the areas to be combined, the operations to be performed when combining the data, and which color planes (R or G or B or R, G, and B) are used, use the CombineExt method.

Once a region is defined, you can create a mask from it, which you can save in the alpha channel of a 16- or 32-bit file. Refer to the BitmapAlpha property for an example of how to save a region in an alpha channel.

image\sqrblit.gif Transparent control. You can make the background of the LEAD control transparent by setting the Transparent property to TRUE. This lets you take advantage of related transparency properties when one control is positioned on top of another one.

When specifying a transparent color, you can refer to a particular palette index, instead of the RGB value. This can be useful in cases where a color is used for transparency and the palette may contain the same color in more than one palette position.

When specifying a palette index as a color value, use the following formula:

16777216 + index

When evaluating a value to see if it is a palette index, you can use the following logic:

if value >= 16777216 then
index = value - 16777216

(Document/Medical only) Setting Transparent Colors For Annotation Bitmap Objects

The annotation Stamp object (ANN_OBJECT_STAMP, which includes the different Rubber Stamp tools) and Point object (ANN_OBJECT_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. Call the AnnSetTransparent method to set such objects to use a transparent color. By default the transparent color is white (0x00FFFFFF). Use the AnnSetTransparentColor method to set the color to be used as the transparent color. Use the AnnGetTransparent method to get a value that indicates whether the bitmap being used by the annotation object is using a transparent color. Use the AnnGetTransparentColor method to get a value that indicates which color is being used as the transparent color.

FeatherAlphaBlend is a powerful function that can be used to implement transparency, using a mask. In many applications, a transparency mask is saved as an alpha channel. If a bitmap contains a transparency mask in the alpha channel, use BitmapAlpha property 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 FeatherAlphaBlend method as the mask. The following code demonstrates how to do this:

VB Example:

Private Sub Test_Click()
Dim RasterIO As New LEADRasterIO
Dim RasterProc As New LEADRasterProcess
Dim nRet As Integer
Dim RasterAlpha As New LEADRaster
Dim RasterDst As New LEADRaster

' Load a bitmap at 16 bits per pixel
nRet = RasterIO.Load(LEADRasterView1.Raster, "c:\parrots.jpg", 16, 0, 1)
' Specify a rectangle to define the region
nRet = LEADRasterView1.Raster.SetRgnEllipse(0, 0, 100, 100, L_RGN_SET)
' Create a mask bitmap from the region
nRet = LEADRasterView1.Raster.CreateMaskFromRgn()
' Update the alpha channel in the main bitmap
RasterAlpha.Bitmap = LEADRasterView1.Raster.MaskBitmap
' Save the bitmap at 16 bits per pixel to keep the alpha channel
nRet = RasterIO.Save (LEADRasterView1.Raster, "TEST.TIF", FILE_TIF, 16, 0, 0)
' Load the bitmap that we just saved and get its alpha channel
nRet = RasterIO.Load(LEADRasterView1.Raster, "TEST.tif", 16, 0, 1)
RasterDst.CreateBitmap LEADRasterView1.Raster.BitmapWidth, LEADRasterView1.Raster.BitmapHeight, LEADRasterView1.Raster.BitmapBits
nRet = RasterProc.FeatherAlphaBlend(RasterDst, 0, 0, RasterAlpha.BitmapWidth, RasterAlpha.BitmapHeight, LEADRasterView1.Raster, 0, 0, RasterAlpha)
LEADRasterView2.Raster = RasterDst
End Sub

VC++ Example:

ILEADRasterIO *pRasterIO = NULL;
ILEADRasterProcess *pRasterProc = NULL;
ILEADRaster *pRasterAlpha;
ILEADRaster *pRasterDst;
int nRet;


CoCreateInstance(
CLSID_LEADRaster,
NULL,
CLSCTX_ALL,
IID_ILEADRaster,
(void**)&pRasterAlpha
);

CoCreateInstance(
CLSID_LEADRaster,
NULL,
CLSCTX_ALL,
IID_ILEADRaster,
(void**)&pRasterDst
);

CoCreateInstance(
CLSID_LEADRasterIO,
NULL,
CLSCTX_ALL,
IID_ILEADRasterIO,
(void**)&pRasterIO
);

CoCreateInstance(
CLSID_LEADRasterProcess,
NULL,
CLSCTX_ALL,
IID_ILEADRasterProcess,
(void**)&pRasterProc
);



//Load a bitmap at 16 bits per pixel
nRet = pRasterIO->Load(m_LEADRasterView1.GetRaster (), "c:\\parrots.jpg", 16, 0, 1);
// Specify a rectangle to define the region
nRet = m_LEADRasterView1.GetRaster().SetRgnEllipse(0, 0, 100, 100, L_RGN_SET);
//Create a mask bitmap from the region
nRet = m_LEADRasterView1.GetRaster().CreateMaskFromRgn();
//Update the alpha channel in the main bitmap
pRasterAlpha->PutBitmap(m_LEADRasterView1.GetRaster().GetMaskBitmap());
// Save the bitmap at 16 bits per pixel to keep the alpha channel
nRet = pRasterIO->Save (m_LEADRasterView1.GetRaster(), "TEST.TIF", FILE_TIF, 16, (QFactorConstants)0, 0);
// Load the bitmap that we just saved and get its alpha channel
nRet = pRasterIO->Load(m_LEADRasterView1.GetRaster(), "TEST.tif", 16, (QFactorConstants)0, 1);

pRasterDst->CreateBitmap(m_LEADRasterView1.GetRaster().GetBitmapWidth(), m_LEADRasterView1.GetRaster().GetBitmapHeight(), m_LEADRasterView1.GetRaster().GetBitmapBits());
nRet = pRasterProc->FeatherAlphaBlend(pRasterDst, 0, 0, pRasterAlpha->BitmapWidth, pRasterAlpha->BitmapHeight, m_LEADRasterView1.GetRaster (), 0, 0, pRasterAlpha);
m_LEADRasterView2.GetRaster().SetBitmap(pRasterDst->GetBitmap());
pRasterIO->Release();
pRasterProc ->Release();
pRasterAlpha->Release();
pRasterDst->Release();