BitmapAlpha example for Delphi
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.
var
sRet: smallint;
rgntop: Single;
rgnleft: Single;
rgnwidth: Single;
rgnheight: Single;
hRGN: Longint;
RasterIO:LEADRasterIO;
RasterProc:LEADRasterProcess;
begin
RasterIO:= CreateComObject (CLASS_LEADRasterIO) As LEADRasterIO;
RasterProc:= CreateComObject ( CLASS_LEADRasterProcess) As LEADRasterProcess;
LEADRasterView1.ScaleMode := 3;
RasterIO.Load ( LEADRasterView1.Raster, '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;
RasterProc.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
RasterProc.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
RasterIO.Save ( LEADRasterView1.Raster, 'd:\pictures\test.tga', FILE_TGA, 32, 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
RasterIO.Load ( LEADRasterView1.Raster, '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 ( sRet ) ;
end;