Available in the LEADTOOLS Imaging toolkit. |
BitmapAlpha example for C++ 4.0 and later
This example shows how to use the BitmapAlpha property to save a region in a file's alpha channel. It also shows how to use the region as a mask for transparency.
float rgntop;
float rgnleft;
float rgnwidth;
float rgnheight;
long hRGN;
long test;
m_LEAD1.SetScaleMode (3);
m_LEAD1.Load ("v:\\images\\eagle.cmp", 0, 0, 1);
m_LEAD1.SetAutoRepaint (FALSE);
// Get an alpha channel bitmap, and fill it with white
m_LEADAlpha.SetBitmap (m_LEAD1.GetBitmapAlpha ());
m_LEADAlpha.SetRgnColor ( RGB(0, 0, 0), L_RGN_SETNOT);
test = m_LEADAlpha.GetRgnArea ();
// test here, if test = 0, then Alpha is all Black
if (test == 0)
AfxMessageBox("Alpha image is all black");
m_LEADAlpha.Fill (RGB(255, 255, 255));
rgntop = 0;
rgnleft = 0;
rgnwidth = m_LEAD1.GetBitmapWidth ();
rgnheight = m_LEAD1.GetBitmapHeight ();
// Create an elliptical region in the AlphaBitmap
m_LEADAlpha.SetRgnEllipse (rgntop, rgnleft, rgnwidth, rgnheight, L_RGN_SET);
// Fill the region with black
m_LEADAlpha.Fill (RGB(0, 0, 0));
// Free the region
m_LEADAlpha.FreeRgn ();
// Update the alpha channel in the main bitmap
m_LEAD1.SetBitmapAlpha (m_LEADAlpha.GetBitmap ());
// Save the bitmap as 32-bit TGA
m_LEAD1.Save("d:\\temp\\test.tga", FILE_TGA, 32, 0, SAVE_OVERWRITE);
// Free the bitmaps
m_LEAD1.SetBitmap (0);
m_LEADAlpha.SetBitmap(0);
// Load the bitmap we just saved and get the alpha channel
m_LEAD1.Load ("d:\\temp\\test.tga", 0, 0, 1);
m_LEADAlpha.SetBitmap(m_LEAD1.GetBitmapAlpha ());
// Create a region in the AlphaBitmap that includes all of the black pixels
m_LEADAlpha.SetRgnColor (RGB(0, 0, 0), L_RGN_SET);
// Get the region from the AlphaBitmap and set it into the main bitmap
hRGN = m_LEADAlpha.GetRgnHandle ();
m_LEAD1.SetRgnHandle (hRGN, 0, 0, L_RGN_SET);
m_LEAD1.SetPaintRgnOnly (TRUE);
m_LEAD1.ForceRepaint ();