What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleMake a mask image for a picture with a "transparent" color
DescriptionThis example shows how to make a mask image for a picture with a "transparent" color in Visual Basic 6. It uses the GetDIBits and SetDIBits API functions to manipulate the image as a device-independent bitmap (DIB).
Keywordsmask, transparent image, transparent color
CategoriesGraphics, API
This program makes a mask for an image that contains a "transparent" color. For example, suppose the image should be transparent where it is green and visible elsewhere. The program would use this routine to convert all green pixels to white and all other pixels to black. It could then use that mask to overlay the picture on another picture.

The MakeMask subroutine uses the GetDIBits API function to load the source image into a device-independent bitmap (DIB). It processes the image's pixels and uses the SetDIBits API function to save the results into a mask PictureBox.

Private Sub MakeMask(ByVal from_picture As PictureBox, _
    ByVal mask_picture As PictureBox, ByVal from_color As _
    OLE_COLOR, ByVal to_color As OLE_COLOR, ByVal _
    other_color As OLE_COLOR)
Dim bitmap_info As BITMAPINFO
Dim pixels() As Byte
Dim bytes_per_scanLine As Integer
Dim pad_per_scanLine As Integer
Dim X As Integer
Dim Y As Integer
Dim r As Byte
Dim g As Byte
Dim b As Byte
Dim to_r As Byte
Dim to_g As Byte
Dim to_b As Byte
Dim other_r As Byte
Dim other_g As Byte
Dim other_b As Byte

    ' Make RGBTriplet values for the colors.
    UnRGB to_color, to_r, to_g, to_b
    UnRGB other_color, other_r, other_g, other_b
    UnRGB from_color, r, g, b

    ' Prepare the bitmap description.
    from_picture.ScaleMode = vbPixels
    With bitmap_info.bmiHeader
        .biSize = 40
        .biWidth = from_picture.ScaleWidth
        ' Use negative height to scan top-down.
        .biHeight = -from_picture.ScaleHeight
        .biPlanes = 1
        .biBitCount = 32
        .biCompression = BI_RGB
        bytes_per_scanLine = ((((.biWidth * .biBitCount) + _
            31) \ 32) * 4)
        pad_per_scanLine = bytes_per_scanLine - (((.biWidth _
            * .biBitCount) + 7) \ 8)
        .biSizeImage = bytes_per_scanLine * Abs(.biHeight)
    End With

    ' Load the bitmap's data.
    ReDim pixels(1 To 4, 1 To from_picture.ScaleWidth, 1 To _
    GetDIBits from_picture.hdc, from_picture.Image, _
        0, from_picture.ScaleHeight, pixels(1, 1, 1), _
        bitmap_info, DIB_RGB_COLORS

    ' Modify the pixels.
    For Y = 1 To from_picture.ScaleHeight
        For X = 1 To from_picture.ScaleWidth
            ' See if we need to change this pixel.
            If (pixels(pixR, X, Y) = r) And _
               (pixels(pixG, X, Y) = g) And _
               (pixels(pixB, X, Y) = b) _
                pixels(pixR, X, Y) = to_r
                pixels(pixG, X, Y) = to_g
                pixels(pixB, X, Y) = to_b
                pixels(pixR, X, Y) = other_r
                pixels(pixG, X, Y) = other_g
                pixels(pixB, X, Y) = other_b
            End If
        Next X
    Next Y

    ' Display the result.
    SetDIBits picMask.hdc, picMask.Image, _
        0, from_picture.ScaleHeight, pixels(1, 1, 1), _
        bitmap_info, DIB_RGB_COLORS
    picMask.Picture = picMask.Image
    picMask.Visible = True
End Sub
For more information on graphics programming in Visual Basic, see my book Visual Basic Graphics Programming.
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.