How to Create a Transparent Bitmap Using Visual Basic (94961)



The information in this article applies to:

  • Microsoft Visual Basic Standard Edition for Windows 2.0
  • Microsoft Visual Basic Standard Edition for Windows 3.0

This article was previously published under Q94961

SUMMARY

A transparent image shows the background behind it instead of the image itself. You can use an icon editor such as the IconWorks sample program provided with Visual Basic to create icons that contain transparent parts. This article shows you how to make certain parts of a bitmap transparent.

MORE INFORMATION

Here are the six general steps required to create a transparent bitmap:
  1. Store the area, or background, where the bitmap is going to be drawn.
  2. Create a monochrome mask of the bitmap that identifies the transparent areas of the bitmap by using a white pixel to indicate transparent areas and a black pixel to indicate non-transparent areas of the bitmap.
  3. Combine the pixels of the monochrome mask with the background bitmap using the And binary operator. The area of the background where the non-transparent portion of the bitmap will appear is made black.
  4. Combine an inverted copy of the monochrome mask (step 2) with the source bitmap using the And binary operator. The transparent areas of the source bitmap will be made black.
  5. Combine the modified background (step 3) with the modified source bitmap (step 4) using the Xor binary operator. The background will show through the transparent portions of the bitmap.
  6. Copy the resulting bitmap to the background

Example Code

  1. Start Visual Basic or from the File menu, choose New Project (ALT, F, N) if Visual Basic is already running. Form1 is created by default.
  2. Add the following controls to Form1 with the associated property values:
       Control          Name (or CtlName)  Property Settings
       -----------------------------------------------------------------
       Picture          pictSource         Picture ="WINDOWS\THATCH.BMP"
       Picture          pictDest           Picture ="WINDOWS\ARCHES.BMP"
       Command button   cmdCopy            Caption ="Copy"
    
    						
  3. From the File menu, choose New Module (ALT, F, M). Module1 is created.
  4. Add the following code to the cmdCopy_Click event procedure of Form1. This code calls the TransparentBlt() function to copy a source bitmap to a destination (background) picture control. White (QBColor(15)) areas of the bitmap are made transparent against the background bitmap.
       Sub cmdCopy_Click ()
         Call TransparentBlt(pictDest, pictSource.Picture, 10, 10, QBColor(15))
       End Sub
    
    						
  5. Add the following code the general declarations section of Module1. Enter each Declare statement as one, single line:
       Type bitmap
          bmType As Integer
          bmWidth As Integer
          bmHeight As Integer
          bmWidthBytes As Integer
          bmPlanes As String * 1
          bmBitsPixel As String * 1
          bmBits As Long
       End Type
    
       Declare Function BitBlt Lib "GDI" (ByVal srchDC As Integer, ByVal srcX
          As Integer, ByVal srcY As Integer, ByVal srcW As Integer, ByVal srcH
          As Integer, ByVal desthDC As Integer, ByVal destX As Integer, ByVal
          destY As Integer, ByVal op As Long) As Integer
       Declare Function SetBkColor Lib "GDI" (ByVal hDC As Integer, ByVal
          cColor As Long) As Long
       Declare Function CreateCompatibleDC Lib "GDI" (ByVal hDC As Integer)
          As Integer
       Declare Function DeleteDC Lib "GDI" (ByVal hDC As Integer) As Integer
       Declare Function CreateBitmap Lib "GDI" (ByVal nWidth As Integer, ByVal
          nHeight As Integer, ByVal cbPlanes As Integer, ByVal cbBits As
          Integer, lpvBits As Any) As Integer
       Declare Function CreateCompatibleBitmap Lib "GDI" (ByVal hDC As Integer,
          ByVal nWidth As Integer, ByVal nHeight As Integer) As Integer
       Declare Function SelectObject Lib "GDI" (ByVal hDC As Integer, ByVal
          hObject As Integer) As Integer
       Declare Function DeleteObject Lib "GDI" (ByVal hObject As Integer) As
          Integer
       Declare Function GetObj Lib "GDI" Alias "GetObject" (ByVal hObject As
          Integer, ByVal nCount As Integer, bmp As Any) As Integer
       Const SRCCOPY = &HCC0020
       Const SRCAND = &H8800C6
       Const SRCPAINT = &HEE0086
       Const NOTSRCCOPY = &H330008
    
    						
  6. Add the following Sub procedure to the general declarations section of Module1. TransparentBlt() accepts six parameters: a destination picture control (dest), a source bitmap to become transparent (srcBmp), the X,Y coordinates in pixels where you want to place the source bitmap on the destination (destX and destY), and the RGB value for the color you want to be transparent. TransparentBlt() copies the source bitmap to any X,Y location on the background making areas transparent.
       Sub TransparentBlt (dest As Control, ByVal srcBmp As Integer, ByVal
          destX As Integer, ByVal destY As Integer, ByVal TransColor As Long)
          Const PIXEL = 3
          Dim destScale As Integer
          Dim srcDC As Integer  'source bitmap (color)
          Dim saveDC As Integer 'backup copy of source bitmap
          Dim maskDC As Integer 'mask bitmap (monochrome)
          Dim invDC As Integer  'inverse of mask bitmap (monochrome)
          Dim resultDC As Integer 'combination of source bitmap & background
          Dim bmp As bitmap 'description of the source bitmap
          Dim hResultBmp As Integer 'Bitmap combination of source & background
          Dim hSaveBmp As Integer 'Bitmap stores backup copy of source bitmap
          Dim hMaskBmp As Integer 'Bitmap stores mask (monochrome)
          Dim hInvBmp As Integer  'Bitmap holds inverse of mask (monochrome)
          Dim hPrevBmp As Integer 'Bitmap holds previous bitmap selected in DC
          Dim hSrcPrevBmp As Integer  'Holds previous bitmap in source DC
          Dim hSavePrevBmp As Integer 'Holds previous bitmap in saved DC
          Dim hDestPrevBmp As Integer 'Holds previous bitmap in destination DC
          Dim hMaskPrevBmp As Integer 'Holds previous bitmap in the mask DC
          Dim hInvPrevBmp As Integer 'Holds previous bitmap in inverted mask DC
          Dim OrigColor As Long 'Holds original background color from source DC
          Dim Success As Integer 'Stores result of call to Windows API
          If TypeOf dest Is PictureBox Then 'Ensure objects are picture boxes
            destScale = dest.ScaleMode 'Store ScaleMode to restore later
            dest.ScaleMode = PIXEL 'Set ScaleMode to pixels for Windows GDI
            'Retrieve bitmap to get width (bmp.bmWidth) & height (bmp.bmHeight)
            Success = GetObj(srcBmp, Len(bmp), bmp)
            srcDC = CreateCompatibleDC(dest.hDC)    'Create DC to hold stage
            saveDC = CreateCompatibleDC(dest.hDC)   'Create DC to hold stage
            maskDC = CreateCompatibleDC(dest.hDC)   'Create DC to hold stage
            invDC = CreateCompatibleDC(dest.hDC)    'Create DC to hold stage
            resultDC = CreateCompatibleDC(dest.hDC) 'Create DC to hold stage
            'Create monochrome bitmaps for the mask-related bitmaps:
            hMaskBmp = CreateBitmap(bmp.bmWidth, bmp.bmHeight, 1, 1, ByVal 0&)
            hInvBmp = CreateBitmap(bmp.bmWidth, bmp.bmHeight, 1, 1, ByVal 0&)
            'Create color bitmaps for final result & stored copy of source
            hResultBmp = CreateCompatibleBitmap(dest.hDC, bmp.bmWidth,
               bmp.bmHeight)
            hSaveBmp = CreateCompatibleBitmap(dest.hDC, bmp.bmWidth,
               bmp.bmHeight)
            hSrcPrevBmp = SelectObject(srcDC, srcBmp)     'Select bitmap in DC
            hSavePrevBmp = SelectObject(saveDC, hSaveBmp) 'Select bitmap in DC
            hMaskPrevBmp = SelectObject(maskDC, hMaskBmp) 'Select bitmap in DC
            hInvPrevBmp = SelectObject(invDC, hInvBmp)    'Select bitmap in DC
            hDestPrevBmp = SelectObject(resultDC, hResultBmp) 'Select bitmap
            Success = BitBlt(saveDC, 0, 0, bmp.bmWidth, bmp.bmHeight, srcDC,
               0, 0, SRCCOPY) 'Make backup of source bitmap to restore later
            'Create mask: set background color of source to transparent color.
            OrigColor = SetBkColor(srcDC, TransColor)
            Success = BitBlt(maskDC, 0, 0, bmp.bmWidth, bmp.bmHeight, srcDC,
               0, 0, SRCCOPY)
            TransColor = SetBkColor(srcDC, OrigColor)
            'Create inverse of mask to AND w/ source & combine w/ background.
            Success = BitBlt(invDC, 0, 0, bmp.bmWidth, bmp.bmHeight, maskDC,
              0, 0, NOTSRCCOPY)
            'Copy background bitmap to result & create final transparent bitmap
            Success = BitBlt(resultDC, 0, 0, bmp.bmWidth, bmp.bmHeight,
               dest.hDC, destX, destY, SRCCOPY)
            'AND mask bitmap w/ result DC to punch hole in the background by
            'painting black area for non-transparent portion of source bitmap.
            Success = BitBlt(resultDC, 0, 0, bmp.bmWidth, bmp.bmHeight,
               maskDC, 0, 0, SRCAND)
            'AND inverse mask w/ source bitmap to turn off bits associated
            'with transparent area of source bitmap by making it black.
            Success = BitBlt(srcDC, 0, 0, bmp.bmWidth, bmp.bmHeight, invDC,
               0, 0, SRCAND)
            'XOR result w/ source bitmap to make background show through.
            Success = BitBlt(resultDC, 0, 0, bmp.bmWidth, bmp.bmHeight,
               srcDC, 0, 0, SRCPAINT)
            Success = BitBlt(dest.hDC, destX, destY, bmp.bmWidth, bmp.bmHeight,
               resultDC, 0, 0, SRCCOPY) 'Display transparent bitmap on backgrnd
            Success = BitBlt(srcDC, 0, 0, bmp.bmWidth, bmp.bmHeight, saveDC,
               0, 0, SRCCOPY) 'Restore backup of bitmap.
            hPrevBmp = SelectObject(srcDC, hSrcPrevBmp) 'Select orig object
            hPrevBmp = SelectObject(saveDC, hSavePrevBmp) 'Select orig object
            hPrevBmp = SelectObject(resultDC, hDestPrevBmp) 'Select orig object
            hPrevBmp = SelectObject(maskDC, hMaskPrevBmp) 'Select orig object
            hPrevBmp = SelectObject(invDC, hInvPrevBmp) 'Select orig object
            Success = DeleteObject(hSaveBmp)   'Deallocate system resources.
            Success = DeleteObject(hMaskBmp)   'Deallocate system resources.
            Success = DeleteObject(hInvBmp)    'Deallocate system resources.
            Success = DeleteObject(hResultBmp) 'Deallocate system resources.
            Success = DeleteDC(srcDC)          'Deallocate system resources.
            Success = DeleteDC(saveDC)         'Deallocate system resources.
            Success = DeleteDC(invDC)          'Deallocate system resources.
            Success = DeleteDC(maskDC)         'Deallocate system resources.
            Success = DeleteDC(resultDC)       'Deallocate system resources.
            dest.ScaleMode = destScale 'Restore ScaleMode of destination.
          End If
       End Sub
    
    						
  7. From the Run menu, choose Start (ALT, R, S) to run the program.
  8. Click the Copy button. The thatched pattern in the first picture is copied onto the second picture (an image of arches) making the arches show through areas of the previously white thatched pattern.

Modification Type:MajorLast Reviewed:10/30/2003
Keywords:kbcode KB94961