BitmapAlpha example for C++ Builder

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 int hRGN;
   LEADRasterIO* pRasterIO= NULL;
   LEADRasterProcess* pRasterProc= NULL;


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

   LEADRasterView1->ScaleMode = 3;
   pRasterIO->Load ( LEADRasterView1->Raster, AnsiToOLESTR("v:\\images\\image1.cmp"), 0, 0, 1 ) ;
   LEADRasterView1->AutoRepaint = False;

   //Get an alpha channel bitmap, and fill it with white
   LEADRasterView2->Raster->Bitmap = LEADRasterView1->Raster->BitmapAlpha;
   pRasterProc->Fill ( LEADRasterView2->Raster, RGB(255, 255, 255)) ;
   rgntop = 0;
   rgnleft = 0;
   rgnwidth = LEADRasterView1->Raster->BitmapWidth;
   rgnheight = LEADRasterView1->Raster->BitmapHeight;
   //Create an elliptical region in the AlphaBitmap
   LEADRasterView2->Raster->SetRgnEllipse ( rgntop, rgnleft, rgnwidth, rgnheight, L_RGN_SET ) ;
   //Fill the region with black
   pRasterProc->Fill ( LEADRasterView2->Raster, RGB(0, 0, 0)) ;
   //Free the region
   LEADRasterView2->Raster->FreeRgn ( ) ;
   //Update the alpha channel in the main bitmap
   LEADRasterView1->Raster->BitmapAlpha = LEADRasterView2->Raster->Bitmap;
   //Save the bitmap as 32-bit TGA
   pRasterIO->Save ( LEADRasterView1->Raster, AnsiToOLESTR("d:\\pictures\\test.tga"), FILE_TGA, 32, (QFactorConstants)0, SAVE_OVERWRITE ) ;
   //Free the bitmaps
   LEADRasterView1->Raster->Bitmap = 0;
   LEADRasterView2->Raster->Bitmap = 0;
   //Load the bitmap we just saved and get the alpha channel
   pRasterIO->Load ( LEADRasterView1->Raster, AnsiToOLESTR("d:\\pictures\\test.tga"), 0, 0, 1 ) ;
   LEADRasterView2->Raster->Bitmap = LEADRasterView1->Raster->BitmapAlpha;
   //Create a region in the AlphaBitmap that includes all of the black pixels
   LEADRasterView2->Raster->SetRgnColor ( RGB(0, 0, 0), L_RGN_SET ) ;
   //Get the region from the AlphaBitmap and set it into the main bitmap
   hRGN = LEADRasterView2->Raster->GetRgnHandle ( ) ;
   LEADRasterView1->Raster->SetRgnHandle ( hRGN, 0, 0, L_RGN_SET ) ;
   LEADRasterView2->Raster->DeleteRgnHandle ( hRGN ) ;
   LEADRasterView1->PaintRgnOnly = True;
   LEADRasterView1->ForceRepaint ( );
   pRasterProc-> Release( );
   pRasterIO-> Release ( );