Implementing Transparency

You can implement transparency when doing any of the following:

Once a region is defined, you can create a mask from it, using the L_CreateMaskFromBitmapRgn function, which you can save in the alpha channel of a 16- or 32-bit file. Refer to the L_SetBitmapAlpha 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.

L_FeatherAlphaBlendBitmap 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, 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. The following code demonstrates how to do this:

void TestFunction(HWND hWnd, pBITMAPHANDLE pDstBitmap) 
{ 
   BITMAPHANDLE MainBitmap;   /* Main bitmap */ 
   BITMAPHANDLE AlphaBitmap;  /* Alpha channel bitmap */ 
   RGNXFORM XFormToBitmap;    /* Structure for transforming display coordinates */ 
   HDC hWindowDC;    /* Device context of the current window */ 
   RECT rClientArea; /* Client area of the current window */ 
   RECT rRgnRect;    /* Rectangle that defines the current region */ 
   HPALETTE hSavedPalette = NULL;   /* Temporary copy of the current system palette */ 
   HPALETTE hOurPalette = NULL;     /* The palette that we will use to paint */ 
   L_INT nRet; 
   /* Get the device context of the current window */ 
   hWindowDC = GetDC (hWnd); 
   /* Get the client area of the current window */ 
   GetClientRect(hWnd,&rClientArea); 
   /* Load a bitmap at 16 bits per pixel */ 
   nRet = L_LoadBitmap("TEST.bmp", &MainBitmap, sizeof(BITMAPHANDLE), 16, ORDER_BGR, NULL, NULL); 
   /* Set XFormToBitmap fields, assuming that the display rectangle is the same 
   as the client area of the current window */ 
   XFormToBitmap.uViewPerspective = TOP_LEFT; 
   XFormToBitmap.nXScalarNum = BITMAPWIDTH(&MainBitmap); 
   XFormToBitmap.nXScalarDen = rClientArea.right; 
   XFormToBitmap.nYScalarNum = BITMAPHEIGHT(&MainBitmap); 
   XFormToBitmap.nYScalarDen = rClientArea.bottom; 
   XFormToBitmap.nXOffset = 0; 
   XFormToBitmap.nYOffset = 0; 
   /* Specify a rectangle to define the region */ 
   SetRect(&rRgnRect, rClientArea.right/8, rClientArea.bottom/8, 
   rClientArea.right/2, rClientArea.bottom/2); 
   /* Create an elliptical region in the AlphaBitmap */ 
   nRet = L_SetBitmapRgnEllipse(&MainBitmap, &XFormToBitmap, &rRgnRect, L_RGN_SET); 
   /* Create a mask bitmap from the region */ 
   nRet = L_CreateMaskFromBitmapRgn ( &MainBitmap, &AlphaBitmap, sizeof(BITMAPHANDLE)  ); 
   /* Update the alpha channel in the main bitmap */ 
   nRet = L_SetBitmapAlpha(&MainBitmap, &AlphaBitmap); 
   /* Save the bitmap at 16 bits per pixel to keep the alpha channel */ 
   nRet = L_SaveBitmap("TEST.TIF", &MainBitmap, FILE_TIF, 16, 0, NULL); 
    
   /* Free the bitmaps */ 
   L_FreeBitmap(&MainBitmap); 
   L_FreeBitmap(&AlphaBitmap); 
   /* Load the bitmap that we just saved and get its alpha channel */ 
   nRet = L_LoadBitmap("TEST.TIF", &MainBitmap, sizeof(BITMAPHANDLE), 0, ORDER_BGR, NULL, NULL); 
   nRet = L_GetBitmapAlpha(&MainBitmap, &AlphaBitmap, sizeof(BITMAPHANDLE)); 
    
   nRet = L_FeatherAlphaBlendBitmap(pDstBitmap, 0, 0, AlphaBitmap.Width, AlphaBitmap.Height, &MainBitmap, 0, 0, &AlphaBitmap, 0, 0, 0); 
   L_FreeBitmap(&MainBitmap); 
   L_FreeBitmap(&AlphaBitmap); 
   return; 
} 

Setting Transparent Colors For Annotation Bitmap Objects (Document and Medical Imaging 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. Call the L_AnnSetTransparent function to set such objects to use a transparent color. By default the transparent color is white (0x00FFFFFF). Use the L_AnnSetTransparentColor function to set the color to be used as the transparent color. Use the L_AnnGetTransparent function to get a value that indicates whether the bitmap being used by the annotation object is using a transparent color. Use the L_AnnGetTransparentColor function to get a value that indicates which color is being used as the transparent color.

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

LEADTOOLS Raster Imaging C API Help