• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 568
  • Last Modified:

Transparent Bitmap

How can I display a transparent bitmap without drawing first a mask of the oroginal and painting the background with black in just a code? Please HELP!!!
0
jaron
Asked:
jaron
  • 2
1 Solution
 
Answers2000Commented:
The way I do this in C++ is to use the Image List (in COMMCTRL).  You can make any single color be transparent (so I pick Magenta).

You can call these APIs in VB.  Sorry no time for me to write code in VB to show you how, but may be this is enough to get you started ?
0
 
twardCommented:
Try this Function:

Public Sub TransParentStretchBlt(ByRef dest As Control, ByVal src As Control, ByVal destX As Long, ByVal destY As Long, ByVal TransColor As Long)

  Dim destScale As Long
  Dim srcDC As Long        ' Source bitmap (color)
  Dim saveDC As Long       ' Backup copy of source bitmap
  Dim maskDC As Long       ' Mask bitmap (monochrome)
  Dim invDC As Long        ' Inverse of mask bitmap (monochrome)
  Dim resultDC As Long     ' Combination of source bitmap & background
  Dim bmp As BITMAP        ' Description of the source bitmap
  Dim hResultBmp As Long   ' Bitmap combination of source & background
  Dim hSaveBmp As Long     ' Bitmap stores backup copy of source bitmap
  Dim hMaskBmp As Long     ' Bitmap stores mask (monochrome)
  Dim hInvBmp As Long      ' Bitmap holds inverse of mask (monochrome)
  Dim hPrevBmp As Long     ' Bitmap holds previous bitmap selected in DC
  Dim hSrcPrevBmp As Long  ' Holds previous bitmap in source DC
  Dim hSavePrevBmp As Long ' Holds previous bitmap in saved DC
  Dim hDestPrevBmp As Long ' Holds previous bitmap in destination DC
  Dim hMaskPrevBmp As Long ' Holds previous bitmap in the mask DC
  Dim hInvPrevBmp As Long  ' Holds previous bitmap in inverted mask DC
  Dim OrigColor As Long    ' Holds original background color from source DC
  Dim Success As Long      ' Stores result of call to Windows API
 
  On Error Resume Next
 
  ' Ensure objects are picture boxes '
  If TypeOf dest Is PictureBox Then
   
    dest.Cls
    destScale = dest.ScaleMode    ' Store ScaleMode to restore later
    dest.ScaleMode = vbPixels     ' Set ScaleMode to pixels for Windows GDI

    ' Retrieve bitmap to get width (bmp.bmWidth) & height (bmp.bmHeight) '
    Success = GetObject(src.Picture, 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, src.Picture)         ' 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

    ' Make backup of source bitmap to restore later '
    Success = BitBlt(saveDC, 0, 0, bmp.bmWidth, bmp.bmHeight, srcDC, 0, 0, SRCCOPY)

    ' 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)

    ' Display transparent bitmap on backgrnd '
    Success = StretchBlt(dest.hdc, destX, destY, dest.ScaleWidth, dest.ScaleHeight, resultDC, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY)

    ' Restore backup of bitmap. '
    Success = BitBlt(srcDC, 0, 0, bmp.bmWidth, bmp.bmHeight, saveDC, 0, 0, SRCCOPY)
   
    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
 
  On Error GoTo 0
 
End Sub

0
 
jaronAuthor Commented:

         I've seen those in VB5 examples but I'll try it anyway. It worked but I did not get the result I wanted. Thank you very much!
        How about dragging it around my screen with out flickering?
0
 
jaronAuthor Commented:

         I've seen those in VB5 examples but I'll try it anyway. It worked but I did not get the result I wanted. Thank you very much!
        How about dragging it around my screen with out flickering? Like the one in microsoft Window's paint program?
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now