Solved

Transparent Bitmap

Posted on 1998-09-03
4
503 Views
Last Modified: 2012-06-27
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
Comment
Question by:jaron
  • 2
4 Comments
 
LVL 8

Expert Comment

by:Answers2000
Comment Utility
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
 
LVL 7

Accepted Solution

by:
tward earned 70 total points
Comment Utility
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
 

Author Comment

by:jaron
Comment Utility

         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
 

Author Comment

by:jaron
Comment Utility

         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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now