Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
TitleUse the GetDIBits and SetDIBits API functions to convert a picture to blue scale in three ways
DescriptionThis example shows how to use the GetDIBits and SetDIBits API functions to convert a picture to blue scale in three ways in Visual Basic 6.
KeywordsGetDIBits, SetDIBits, blue scale, picture
CategoriesGraphics, API
 
The ZeroRedGreen, BlueAverage, and KeepBlueBrightness subroutines convert an image into shades of blue in different ways. All use GetDIBits to get the picture's pixel data, modify the data, and then use SetDIBits to update the picture.

Subroutine ZeroRedGreen sets the green and blue components of each pixel to zero. The result is all blue but darker than the original because the red and green contributions to brightness have been removed.

 
For Y = 1 To picColor.ScaleHeight
    For X = 1 To picColor.ScaleWidth
        pixels(pixR, X, Y) = 0
        pixels(pixG, X, Y) = 0
    Next X
Next Y
 

Subroutine BlueAverage sets each pixel's blue component to the average of its red, green, and blue components. It sets the red and green components to zero. The result is darker than the original because each pixel's total color component values is one third its original value.

 
For Y = 1 To picColor.ScaleHeight
    For X = 1 To picColor.ScaleWidth
        pixels(pixB, X, Y) = CByte( _
            (CInt(pixels(pixR, X, Y)) + _
            pixels(pixG, X, Y) + _
            pixels(pixB, X, Y)) \ 3 _
        )
        pixels(pixR, X, Y) = 0
        pixels(pixG, X, Y) = 0
    Next X
Next Y
 
Subroutine KeepBlueBrightness sets each pixel's blue component to the sum of its red, green, and blue components. If this sum is greater than 255, it sets the blue component to 255 and splits the remainder between the red and green components. The result is a brighter shade of blue. This version gives the best result.
 
For Y = 1 To picColor.ScaleHeight
    For X = 1 To picColor.ScaleWidth
        blue_color = CInt(pixels(pixR, X, Y)) + _
            pixels(pixG, X, Y) + _
            pixels(pixB, X, Y)
        If blue_color > 255 Then
            red_color = (blue_color - 255) \ 2
            green_color = red_color
            blue_color = 255
        Else
            red_color = 0
            green_color = 0
        End If
        pixels(pixR, X, Y) = red_color
        pixels(pixG, X, Y) = green_color
        pixels(pixB, X, Y) = blue_color
    Next X
Next Y
 
My book Visual Basic Graphics Programming has a lot more information about graphics.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated