BitmapAlpha example for C++ 5.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;
ILEADRasterIO *pRasterIO=NULL;
ILEADRasterProcess *pRasterProc=NULL;
CoCreateInstance(CLSID_LEADRasterIO, NULL, CLSCTX_ALL, IID_ILEADRasterIO, (void**)&pRasterIO);
CoCreateInstance(CLSID_LEADRasterProcess, NULL, CLSCTX_ALL, IID_ILEADRasterProcess, (void**)&pRasterProc);
m_LEADRasterView1.SetScaleMode (3);
pRasterIO->Load (m_LEADRasterView1.GetRaster (), "v:\\images\\eagle.cmp", 0, 0, 1);
m_LEADRasterView1.SetAutoRepaint (FALSE);
// Get an alpha channel bitmap, and fill it with white
m_LEADRasterView2.GetRaster().SetBitmap (m_LEADRasterView1.GetRaster().GetBitmapAlpha ());
m_LEADRasterView2.GetRaster().SetRgnColor (RGB(0, 0, 0), L_RGN_SETNOT);
test = m_LEADRasterView2.GetRaster().GetRgnArea ();
// test here, if test = 0, then Alpha is all Black
if (test == 0)
AfxMessageBox(TEXT("Alpha image is all black"));
pRasterProc->Fill (m_LEADRasterView2.GetRaster(), RGB(255, 255, 255));
rgntop = 0;
rgnleft = 0;
rgnwidth = m_LEADRasterView1.GetRaster().GetBitmapWidth ();
rgnheight = m_LEADRasterView1.GetRaster().GetBitmapHeight ();
// Create an elliptical region in the AlphaBitmap
m_LEADRasterView2.GetRaster().SetRgnEllipse (rgntop, rgnleft, rgnwidth, rgnheight, L_RGN_SET);
// Fill the region with black
pRasterProc->Fill (m_LEADRasterView2.GetRaster(), RGB(0, 0, 0));
// Free the region
m_LEADRasterView2.GetRaster().FreeRgn ();
// Update the alpha channel in the main bitmap
m_LEADRasterView1.GetRaster().SetBitmapAlpha (m_LEADRasterView2.GetRaster().GetBitmap ());
// Save the bitmap as 32-bit TGA
pRasterIO->Save (m_LEADRasterView1.GetRaster(), "d:\\temp\\test.tga", FILE_TGA, 32, (QFactorConstants)0, SAVE_OVERWRITE);
// Free the bitmaps
m_LEADRasterView1.GetRaster().SetBitmap (0);
m_LEADRasterView2.GetRaster().SetBitmap(0);
// Load the bitmap we just saved and get the alpha channel
pRasterIO->Load (m_LEADRasterView1.GetRaster(), "d:\\temp\\test.tga", 0, 0, 1);
m_LEADRasterView2.GetRaster().SetBitmap (m_LEADRasterView1.GetRaster().GetBitmapAlpha ());
// Create a region in the AlphaBitmap that includes all of the black pixels
m_LEADRasterView2.GetRaster().SetRgnColor (RGB(0, 0, 0), L_RGN_SET);
// Get the region from the AlphaBitmap and set it into the main bitmap
hRGN = m_LEADRasterView2.GetRaster().GetRgnHandle ();
m_LEADRasterView1.GetRaster().SetRgnHandle (hRGN, 0, 0, L_RGN_SET);
m_LEADRasterView1.SetPaintRgnOnly (TRUE);
m_LEADRasterView1.ForceRepaint ();
pRasterIO->Release();