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 TransparentColor property and set the UseTransparentColor property. For more information about painting images, refer to Displaying an Image. For painting other objects, refer to Drawing Three-Dimensional Shapes and Text.

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

Note:

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

Delphi Example:

var
   value: Integer;
begin
   Lead1.Load('d:\ltwin14\images\image3.gif', 0, 0, -1);
   value := LEAD1.BitmapTransparentColor;
   {Account for the COLORREF, which can be a palette index or an RGB value}
   If (value AND $1000000) <> 0 Then {Palette index}
      Lead1.TransparentColor := Lead1.BitmapPalette[value AND $FFFFFF]
   Else  {RGB value}
      LEAD1.TransparentColor := value;
   Lead1.UseTransparentColor := True;
   Lead1.ForceRepaint;
end;

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 CombineWarp 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(Pascal):

$1000000 OR index

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

If (value AND $1000000) <> 0 Then {value is a Palette index}
index := value AND $FFFFFF;

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

The annotation Stamp (ANNOBJECT_STAMP, which includes the different Rubber Stamp tools) and Point (ANNOBJECT_POINT) objects can be set to display bitmaps that use a transparent color. A transparent color is a color that is not painted when painting an image. Use 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 and FeatherAlphaBlendExt are powerful methods 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, call 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 or FeatherAlphaBlendExt method as the mask. The following code demonstrates how to do this:

1. For Delphi Users:

var
   nRet: L_INT; 
begin
   { Load a bitmap as 16 bits per pixel }
   nRet:= LEADMainImage.Load ('Test.bmp', 16, 1, 1); 
   if(nRet = SUCCESS)then
   begin
      { Let the ViewPerspective = TOP_LEFT }
      LEADMainImage.InfoViewPerspective:= TOP_LEFT; 
      { Create a rgn }
      LEADMainImage.SetRgnEllipse(Trunc(LEADMainImage.Width/8), 
                                  Trunc(LEADMainImage.Height/8), 
                                  Trunc(LEADMainImage.Width/2), 
                                  Trunc(LEADMainImage.Height/2), 
                                  L_RGN_SET); 
      { Create a mask bitmap from the region }
      nRet:= LEADMainImage.CreateMaskFromBitmapRgn ();
      if(nRet = SUCCESS)then
      begin
         LEADAlphaImage.Bitmap:= LEADMainImage.MaskBitmap
         { Update the alpha channel in the main control }
         LEADMainImage.BitmapAlpha:= LEADAlphaImage.Bitmap; 
         { Save the bitmap at 16 bits per pixel to keep the alpha channel }
         LEADMainImage.Save('Test.tif', FILE_TIF, 16, 0, SAVE_OVERWRITE); 

         { Free the main bitmaps }
         LEADMainImage.Bitmap:= 0; 
         LEADAlphaImage.Bitmap:= 0; 
         { Load the bitmap that we just saved and get its alpha channel }
         nRet:= LEADMainImage.Load ('Test.tif', 0, 1, 1); 
         { Create a Bitmap in the Result Control}
         LEADResultImage.CreateBitmap(LEADMainImage.BitmapWidth
                                      LEADMainImage.BitmapHeight
                                      LEADMainImage.BitmapBits); 
         if(nRet = SUCCESS)then
         begin
            LEADAlphaImage.Bitmap:= LEADMainImage.BitmapAlpha; 
            LEADResultImage.FeatherAlphaBlend(0, 
                                              0, 
                                              LEADAlphaImage.BitmapWidth, 
                                              LEADAlphaImage.BitmapHeight, 
                                              LEADMainImage.Bitmap, 
                                              0, 
                                              0, 
                                              LEADAlphaImage.Bitmap); 
         end; 
      end; 
   end; 
   LEADAlphaImage.Bitmap:= 0; 
   LEADMainImage.Bitmap:= 0; 
end;

2. For C++ Builder Users

{
   int nRet; 

   /* Load a bitmap as 16 bits per pixel */
   nRet= LEADMainImage->Load ("Test.bmp", 16, 1, 1); 
   if(nRet == SUCCESS) 
   {
      /* Let the ViewPerspective = TOP_LEFT */
      LEADMainImage->InfoViewPerspective= TOP_LEFT; 
      /* Create a rgn */
      LEADMainImage->SetRgnEllipse(LEADMainImage->Width/8, 
                                   LEADMainImage->Height/8, 
                                   LEADMainImage->Width/2, 
                                   LEADMainImage->Height/2, 
                                   L_RGN_SET); 
      /* Create a mask bitmap from the region */
      nRet= LEADMainImage->CreateMaskFromBitmapRgn ();
      if(nRet == SUCCESS) 
      {
         LEADAlphaImage->Bitmap= LEADMainImage->MaskBitmap
         /* Update the alpha channel in the main control */
         LEADMainImage->BitmapAlpha= LEADAlphaImage->Bitmap; 
         /* Save the bitmap at 16 bits per pixel to keep the alpha channel */
         LEADMainImage->Save("Test.tif", FILE_TIF, 16, 0, SAVE_OVERWRITE); 

         /* Free the main bitmaps */
         LEADMainImage->Bitmap= 0; 
         LEADAlphaImage->Bitmap= 0; 
         /* Load the bitmap that we just saved and get its alpha channel */
         nRet= LEADMainImage->Load ("Test.tif", 0, 1, 1); 
         /* Create a Bitmap in the Result Control*/
         LEADResultImage->CreateBitmap(LEADMainImage->BitmapWidth
                                       LEADMainImage->BitmapHeight
                                       LEADMainImage->BitmapBits); 
         if(nRet == SUCCESS) 
         {
            LEADAlphaImage->Bitmap= LEADMainImage->BitmapAlpha; 
            LEADResultImage->FeatherAlphaBlend(0, 
                                               0, 
                                               LEADAlphaImage->BitmapWidth, 
                                               LEADAlphaImage->BitmapHeight, 
                                               LEADMainImage->Bitmap, 
                                               0, 
                                               0, 
                                               LEADAlphaImage->Bitmap); 
         }
      }
   }
   LEADAlphaImage->Bitmap= 0; 
   LEADMainImage->Bitmap= 0; 
}