BitmapAlpha example for Visual Basic
Note: Also works with Access 95 and 97.
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.
Dim rgntop As Single
Dim rgnleft As Single
Dim rgnwidth As Single
Dim rgnheight As Single
Dim hRGN As Long
Dim RasterIO As New LEADRasterIO
Dim RasterProc As New LEADRasterProcess
Dim test As Long
LEADRasterView1.ScaleMode = 3
RasterIO.Load LEADRasterView1.Raster, "v:\images\eagle.cmp", 0, 0, 1
LEADRasterView1.AutoRepaint = False
'Get an alpha channel bitmap, and fill it with white
LEADRasterView2.Raster.Bitmap = LEADRasterView1.Raster.BitmapAlpha
LEADRasterView2.Raster.SetRgnColor RGB(0, 0, 0), L_RGN_SETNOT
test = LEADRasterView2.Raster.GetRgnArea
' test here, if test = 0, then Alpha is all Black
If test = 0 Then MsgBox "Alpha image is all black"
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:\temp\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:\temp\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