Available in the LEADTOOLS Imaging toolkit. |
BitmapAlpha example for Visual Basic
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 test As Long
LEAD1.ScaleMode = 3
LEAD1.Load "v:\images\eagle.cmp", 0, 0, 1
LEAD1.AutoRepaint = False
'Get an alpha channel bitmap, and fill it with white
LEAD2.Bitmap = LEAD1.BitmapAlpha
LEAD2.SetRgnColor RGB(0, 0, 0), L_RGN_SETNOT
test = LEAD2.GetRgnArea
' test here is test = 0, then Alpha is all black
If test = 0 Then
MsgBox "Alpha image is all black"
End If
LEAD2.Fill RGB(255, 255, 255)
rgntop = 0
rgnleft = 0
rgnwidth = LEAD1.BitmapWidth
rgnheight = LEAD1.BitmapHeight
'Create an elliptical region in the AlphaBitmap
LEAD2.SetRgnEllipse rgntop, rgnleft, rgnwidth, rgnheight, L_RGN_SET
'Fill the region with black
LEAD2.Fill RGB(0, 0, 0)
'Free the region
LEAD2.FreeRgn
'Update the alpha channel in the main bitmap
LEAD1.BitmapAlpha = LEAD2.Bitmap
'Save the bitmap as 32-bit TGA
LEAD1.Save "test.tga", FILE_TGA, 32, 0, SAVE_OVERWRITE
'Free the bitmaps
LEAD1.Bitmap = 0
LEAD2.Bitmap = 0
'Load the bitmap we just saved and get the alpha channel
LEAD1.Load "test.tga", 0, 0, 1
LEAD2.Bitmap = LEAD1.BitmapAlpha
'Create a region in the AlphaBitmap that includes all of the black pixels
LEAD2.SetRgnColor RGB(0, 0, 0), L_RGN_SET
'Get the region from the AlphaBitmap and set it into the main bitmap
hRGN = LEAD2.GetRgnHandle
LEAD1.SetRgnHandle hRGN, 0, 0, L_RGN_SET
LEAD1.PaintRgnOnly = True
LEAD1.ForceRepaint