Results 1 to 5 of 5

Thread: Masking, Argh!

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2000
    Posts
    12

    Angry

    I am trying to figure out how to "mask" a bitmap to get the background out of the picture... I am aware of a BitBlt API that does this, but I cannot figure it out... I downloaded info from vbapi.net/ref, but couldn't figure out what it meant. Any insight into this API would be greatly appreciated.
    Thanks,
    John
    Building a better gaming world through VB.

  2. #2
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    I made a program that generates these masks... download this example RPG engine to get it, your picture just need to have a white background to convert it correctly...

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2000
    Posts
    12
    I can't figure your masking program out. I load a bitmap into it, with a white background, then it just displays another part of the bitmap with only white... No mask. Maybe I'm using it wrong, any help would be appreciated.
    John
    Building a better gaming world through VB.

  4. #4
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    It shouldn't be hard to use.. however...

    First you need a bitmap where the color white (FFFFFF or RGB(255,255,255) or 16777215) will be transparent. You select this picture in my mask editor and press Generate. In the small preview window you see the ... preview If it's ok press Save to overwrite the original bitmap. That's all... Note that I use a white BG for the bitmaps so the mask has a black BG and white FG (you can also draw transparent the other way around...).

    However, hope this helps...

  5. #5
    Fanatic Member Mad Compie's Avatar
    Join Date
    Aug 2000
    Location
    Kuurne (Belgium)
    Posts
    553
    Hi, here some code I found on DevX.com
    It explains the mask thing (black-white):

    Code:
    Public Sub TransBlt(ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal TransColor As Long)
       '
       ' 32-Bit Transparent BitBlt Function
       ' Written by Karl E. Peterson, 9/20/96.
       ' Portions borrowed and modified from KB.
       '
       ' Parameters ************************************************************
       '   hDestDC:     Destination device context
       '   x, y:        Upper-left destination coordinates (pixels)
       '   nWidth:      Width of destination
       '   nHeight:     Height of destination
       '   hSrcDC:      Source device context
       '   xSrc, ySrc:  Upper-left source coordinates (pixels)
       '   TransColor:  RGB value for transparent pixels, typically &HC0C0C0.
       ' ***********************************************************************
       '
       Dim OrigColor As Long      ' Holds original background color
       Dim OrigMode As Long       ' Holds original background drawing mode
       
       If (GetDeviceCaps(hDestDC, CAPS1) And C1_TRANSPARENT) Then
          '
          ' Some NT machines support this *super* simple method!
          ' Save original settings, Blt, restore settings.
          '
          OrigMode = SetBkMode(hDestDC, NEWTRANSPARENT)
          OrigColor = SetBkColor(hDestDC, TransColor)
          Call BitBlt(hDestDC, x, y, nWidth, nHeight, hSrcDC, xSrc, ySrc, SRCCOPY)
          Call SetBkColor(hDestDC, OrigColor)
          Call SetBkMode(hDestDC, OrigMode)
          
       Else
          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 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 hResultBmp As Long     ' Bitmap combination of source & background
          Dim hSavePrevBmp As Long   ' Holds previous bitmap in saved DC
          Dim hMaskPrevBmp As Long   ' Holds previous bitmap in the mask DC
          Dim hInvPrevBmp As Long    ' Holds previous bitmap in inverted mask DC
          Dim hDestPrevBmp As Long   ' Holds previous bitmap in destination DC
          '
          ' Create DCs to hold various stages of transformation.
          '
          saveDC = CreateCompatibleDC(hDestDC)
          maskDC = CreateCompatibleDC(hDestDC)
          invDC = CreateCompatibleDC(hDestDC)
          resultDC = CreateCompatibleDC(hDestDC)
          '
          ' Create monochrome bitmaps for the mask-related bitmaps.
          '
          hMaskBmp = CreateBitmap(nWidth, nHeight, 1, 1, ByVal 0&)
          hInvBmp = CreateBitmap(nWidth, nHeight, 1, 1, ByVal 0&)
          '
          ' Create color bitmaps for final result & stored copy of source.
          '
          hResultBmp = CreateCompatibleBitmap(hDestDC, nWidth, nHeight)
          hSaveBmp = CreateCompatibleBitmap(hDestDC, nWidth, nHeight)
          '
          ' Select bitmaps into DCs.
          '
          hSavePrevBmp = SelectObject(saveDC, hSaveBmp)
          hMaskPrevBmp = SelectObject(maskDC, hMaskBmp)
          hInvPrevBmp = SelectObject(invDC, hInvBmp)
          hDestPrevBmp = SelectObject(resultDC, hResultBmp)
          '
          ' Make backup of source bitmap to restore later.
          '
          Call BitBlt(saveDC, 0, 0, nWidth, nHeight, hSrcDC, xSrc, ySrc, SRCCOPY)
          '
          ' Create mask: set background color of source to transparent color.
          '
          OrigColor = SetBkColor(hSrcDC, TransColor)
          Call BitBlt(maskDC, 0, 0, nWidth, nHeight, hSrcDC, xSrc, ySrc, SRCCOPY)
          TransColor = SetBkColor(hSrcDC, OrigColor)
          '
          ' Create inverse of mask to AND w/ source & combine w/ background.
          '
          Call BitBlt(invDC, 0, 0, nWidth, nHeight, maskDC, 0, 0, NOTSRCCOPY)
          '
          ' Copy background bitmap to result & create final transparent bitmap.
          '
          Call BitBlt(resultDC, 0, 0, nWidth, nHeight, hDestDC, x, y, SRCCOPY)
          '
          ' AND mask bitmap w/ result DC to punch hole in the background by
          ' painting black area for non-transparent portion of source bitmap.
          '
          Call BitBlt(resultDC, 0, 0, nWidth, nHeight, 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.
          '
          Call BitBlt(hSrcDC, xSrc, ySrc, nWidth, nHeight, invDC, 0, 0, SRCAND)
          '
          ' XOR result w/ source bitmap to make background show through.
          '
          Call BitBlt(resultDC, 0, 0, nWidth, nHeight, hSrcDC, xSrc, ySrc, SRCPAINT)
          '
          ' Display transparent bitmap on background.
          '
          Call BitBlt(hDestDC, x, y, nWidth, nHeight, resultDC, 0, 0, SRCCOPY)
          '
          ' Restore backup of original bitmap.
          '
          Call BitBlt(hSrcDC, xSrc, ySrc, nWidth, nHeight, saveDC, 0, 0, SRCCOPY)
          '
          ' Select original objects back.
          '
          Call SelectObject(saveDC, hSavePrevBmp)
          Call SelectObject(resultDC, hDestPrevBmp)
          Call SelectObject(maskDC, hMaskPrevBmp)
          Call SelectObject(invDC, hInvPrevBmp)
          '
          ' Deallocate system resources.
          '
          Call DeleteObject(hSaveBmp)
          Call DeleteObject(hMaskBmp)
          Call DeleteObject(hInvBmp)
          Call DeleteObject(hResultBmp)
          Call DeleteDC(saveDC)
          Call DeleteDC(invDC)
          Call DeleteDC(maskDC)
          Call DeleteDC(resultDC)
       End If
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width