| Home Search What's New Index Books Links Q & A Newsletter Banners Feedback Tip Jar |
MSDN Visual Basic Community

Title | Convert colors that are mostly red, green, or blue into a new color
Description | This example shows how to convert colors that are mostly red, green, or blue into a new color in Visual Basic 6.
Keywords | mostly color, color, SetDIBits, GetDIBits
Categories | Graphics, API
Subroutine TransformImage uses the GetDIBits API function to quickly load the image's bit data into an array of bytes. For each pixel, the program determines whether the pixel is mostly red, green, or blue. Then if the color matches the target color (e.g. we're searching for mostly red pixels), the program converts the pixel to a new color. For example, this would let you convert all mostly green colors to black. When it has checked all of the pixels, the program uses SetDIBits to copy the transformed pixel data into an output PictureBox.
    Private Sub TransformImage(ByVal from_picture As _
        PictureBox, ByVal to_picture As PictureBox, ByVal _
        to_color As OLE_COLOR, ByVal target_mostly As _
        MostlyEnum)
    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 pixel_mostly As MostlyEnum
    Dim to_red As Byte
    Dim to_green As Byte
    Dim to_blue As Byte

        ' Prepare the bitmap description.
        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 _
            _
            from_picture.ScaleHeight)
        GetDIBits from_picture.hdc, from_picture.Image, _
            0, from_picture.ScaleHeight, pixels(1, 1, 1), _
            bitmap_info, DIB_RGB_COLORS

        ' Get to_color's red, green, and blue components.
        UnRGB to_color, to_red, to_green, to_blue

        ' Modify the pixels.
        For Y = 1 To from_picture.ScaleHeight
            For X = 1 To from_picture.ScaleWidth
                ' See if this pixel is mostly
                ' red, green, or blue.
                If pixels(pixR, X, Y) > pixels(pixG, X, Y) And _
                   pixels(pixR, X, Y) > pixels(pixB, X, Y) _
                Then
                    pixel_mostly = mostly_Red
                ElseIf pixels(pixG, X, Y) > pixels(pixR, X, Y) _
                    And _
                       pixels(pixG, X, Y) > pixels(pixB, X, Y) _
                Then
                    pixel_mostly = mostly_Green
                ElseIf pixels(pixB, X, Y) > pixels(pixR, X, Y) _
                    And _
                       pixels(pixB, X, Y) > pixels(pixG, X, Y) _
                Then
                    pixel_mostly = mostly_Blue
                Else
                    pixel_mostly = mostly_Tied
                End If

                ' See if this is the target color.
                If pixel_mostly = target_mostly Then
                    pixels(pixR, X, Y) = to_red
                    pixels(pixG, X, Y) = to_green
                    pixels(pixB, X, Y) = to_blue
                End If
            Next X
        Next Y

        ' Display the result.
        SetDIBits to_picture.hdc, to_picture.Image, _
            0, to_picture.ScaleHeight, pixels(1, 1, 1), _
            bitmap_info, DIB_RGB_COLORS
        to_picture.Picture = to_picture.Image
    End Sub

| Copyright © 1997-2003 Rocky Mountain Computer Consulting, Inc. All rights reserved. |
|  | Updated |