ATTENTION READERS! Lucky's VB Gaming Site is no longer active. For updated game programming information and tutorials, please visit The Game Programming Wiki!
Transparent Blits Without Masks - By Brian Clark
First off this is the first tutorial I’ve ever written, so please feel free to send comments to
webtroll@aol.com.
NOTE: This API-extension DLL was only included in Win98 and up, but can be USED in Win95. Just download this
DLL and extract it into your windows\system directory and it should work!
Step 1. Declare Statements
Ok the first thing you will need to do is add the following declares statement to a new MODULE.
Declare Function TransparentBlt Lib "msimg32" (ByVal hdcDest As Long, _
ByVal nXOriginDest As Long, ByVal nYOriginDest As Long, ByVal nWidthDest _
As Long, ByVal nHeightDest As Long, ByVal hdcSrc As Long, ByVal nXOriginSrc _
As Long, ByVal nYOriginSrc As Long, ByVal nWidthSrc As Long, ByVal _
nHeightSrc As Long, ByVal crTransparent As Long) As Long
Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, _
ByVal Y As Long) As Long
Step 2. Creating the form
1. Next we need to add a form to our project called frmBlt, and a 2 picture boxes the first named picSource, and
the second named picDest.
2. Add three command buttons to your form, named cmdBlit and cmdExit and cmdLoadBitmaps(Only if your
using the second option below).
3. You also need to declare three variables in the declarations part of your form:
Dim hDCSrc as Long
Dim hdcWidth as Integer
Dim hdcHeight as Integer
Your form should now look something like this!
Step 3. Loading your bitmaps
After you have created your form, you will need to load a bitmap into an hDC, which can be done in two different ways: Picture Box
or Using API calls.
Step 4. Loading a bitmap into a hDC(Using a Picturebox)
You can load your source bitmap at design time(using properties window) or at run time using the LoadPicture function.
I hope all of you know how to load a picture at design time!!! So I will concentrate on using the LoadPicture function.
Put this code into the cmdLoadBitmap button:
picSource.Picture = LoadPicture("Path to your bitmap")
picSource.Refresh
hDCSrc = picSource.hDC
GetDimensions '(We'll get to this later)
Now that wasn't so hard was it!!
Step 4.5. Loading a bitmap into a hDC(Using API)
This way is a little harder but will be much better in the long run!!
1. You will need to add three more declare statements into your Module:
Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal _
hObject As Long) As Long
Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
2. Put this code into the cmdLoadBitmaps button:
hdcSrc = CreateCompatibleDC(frmBlt.hdc)
SelectObject hdcSrc, LoadPicture("Path to Bitmap")
GetDimensions
3. Put some code into the Form_Unload procedure
DeleteDC hdcSrc
Step 5. The GetDimensions Function
1. Add this Type to the Module:
Public Type Bitmap '14 bytes
bmType As Long
bmWidth As Long
bmHeight As Long
bmWidthBytes As Long
bmPlanes As Integer
bmBitsPixel As Integer
bmBits As Long
End Type
Public Const LR_CREATEDIBSECTION = &H2000
Public Const LR_LOADFROMFILE = &H10
And a few more declare statements:
Public Declare Function LoadImage Lib "user32" (ByVal hInst As Long, ByVal lpsz _
As String, ByVal un1 As Long, ByVal n1 As Long, ByVal n2 As Long, ByVal un2 As _
Long) As Long
Public Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
2. Put this code into the form declarations section:
Sub GetDimensions()
Dim dbitmap As Bitmap
Dim hbitmap As Long
Dim filename As String
filename = "path to your bitmap"
hbitmap = LoadImage(ByVal 0&, filename, 0, 0, 0, LR_LOADFROMFILE Or LR_CREATEDIBSECTION)
' Get bitmap information
GetObject hbitmap, Len(dbitmap), dbitmap
hdcHeight = dbitmap.bmHeight
hdcWidth = dbitmap.bmWidth
DeleteObject hbitmap
End Sub
Step 6. The Transparent Blit Without Masks
1. Put the actual code into the cmdBlit button:
'Gets the color of the pixel at 0,0 of your bitmap
Dim TransparentColor As Long
TransparentColor = GetPixel(hdcSrc, 0, 0)
TransparentBlt picDest.hdc, 0, 0, hdcWidth, _
hdcHeight, hdcSrc, 0, 0, hdcWidth, _
hdcHeight, TransparentColor