Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Does this work? It certainly does (although it might take a little time to execute), as you can see in Figure 19.11, where we copy the image in the top picture box (Picture1) to the bottom picture box (Picture2).


Figure 19.11  Copying an image bit by bit.

The code for this example is located in the imagecopy folder on this book’s accompanying CD-ROM. Now that we’ve seen how to work with an image bit by bit, we’ll see how to implement some image effects in the next few topics.

Creating Grayscale Images

We’ve seen how to work with images bit by bit in the previous topic. We’ll augment that in this topic, where we see how to convert color images to grayscale images.

We do this by reading an image into a pixel array, then by converting each of those pixels to gray and writing the pixel array out to a new image. Let’s see how this works. We’ll convert the image in a picture box, Picture1, to grayscale, and display it in a new picture box, Picture2, when the user clicks a command button, Command1. To be able to work pixel by pixel, set each picture box’s ScaleMode property to vbPixel (3).

First, we set up storage space for the image in an array named Pixels, declared in the form’s (General) section:

Const intUpperBoundX = 300
Const intUpperBoundY = 300
Dim Pixels(1 To intUpperBoundX, 1 To intUpperBoundY) As Long

When the user clicks the command button, we store the image in Picture1 into the array Pixels:

Private Sub Command1_Click()
    Dim x, y As Integer

    For x = 1 To intUpperBoundX
        For y = 1 To intUpperBoundY
            Pixels(x, y) = Picture1.Point(x, y)
        Next y
    Next x

Now we’re free to work with the image’s pixels in a new loop (to be efficient, this new loop should be incorporated into the first loop where we read the pixels in, but here we’ll use a new loop to make the image-handling process clear). In that new loop, we first separate out the color values (red, green, and blue) for each pixel. To create a grayscale image, you average those color values and then use the resulting average as the red, green, and blue color values in the new image.

The Point method returns a Long integer holding the red, green, and blue color values (which range from 0 to 255) in hexadecimal: &HBBGGRR. That means we can separate out the red, green, and blue color values, storing them as the bytes bytRed, bytGreen, and bytBlue this way:

Private Sub Command1_Click()
    Dim x, y As Integer
    Dim bytRed, bytGreen, bytBlue As Integer

    For x = 1 To intUpperBoundX
        For y = 1 To intUpperBoundY
            Pixels(x, y) = Picture1.Point(x, y)
        Next y
    Next x

    For x = 1 To intUpperBoundX
        For y = 1 To intUpperBoundY
            bytRed = Pixels(x, y) And &HFF
            bytGreen = ((Pixels(x, y) And &HFF00) / &H100) Mod &H100
            bytBlue = ((Pixels(x, y) And &HFF0000) / &H10000) Mod &H100
…

To convert each pixel to grayscale, we just average its color values. Finally, we display the new image in a second picture box, Picture2:

Private Sub Command1_Click()
    Dim x, y As Integer
    Dim bytRed, bytGreen, bytBlue, bytAverage As Integer
…
    For x = 1 To intUpperBoundX
        For y = 1 To intUpperBoundY
            bytRed = Pixels(x, y) And &HFF
            bytGreen = ((Pixels(x, y) And &HFF00) / &H100) Mod &H100
            bytBlue = ((Pixels(x, y) And &HFF0000) / &H10000) Mod &H100

            bytAverage = (bytRed + bytGreen + bytBlue) / 3
            Pixels(x, y) = RGB(bytAverage, bytAverage, bytAverage)
         Next y
    Next x

    For x = 1 To intUpperBoundX
        For y = 1 To intUpperBoundY
            Picture2.PSet (x, y), Pixels(x, y)
        Next y
    Next x

End Sub

The result of this code appears in Figure 19.12. Although the effect is not terribly obvious in a book with black-and-white images, we’re converting an image from color to grayscale in that figure.


Figure 19.12  Converting an image to grayscale.

The code for this example is located in the imagegrayscale folder on this book’s accompanying CD-ROM.

Lightening Images

The Testing Department is calling. Some of the users of your SuperDuperGraphicsPro program are saying the images in that program are too dark—can you let them lighten them? Hmm, you think—how does that work?

You can lighten images by adding the same positive number to each color value (red, green, and blue) of each pixel. Let’s see how this works in an example. Here, we’ll take the image in a picture box, Picture1, and add a value specified by the user to each color value when the user clicks a command button, Command1, displaying the result in a second picture box, Picture2. To be able to work pixel by pixel, set each picture box’s ScaleMode property to vbPixel (3). We’ll also have a text box, Text1, that will hold the value the user wants to add to each color value to lighten it.

We start by setting up the storage we’ll need for the image:

Const intUpperBoundX = 200
Const intUpperBoundY = 200
Dim Pixels(1 To intUpperBoundX, 1 To intUpperBoundY) As Long

Next, we place the value the user wants added to each color value in a new variable named intAddOn when the user clicks the command button:

Private Sub Command1_Click()
    Dim intAddOn As Integer

    intAddOn = Val(Text1.Text)

Now we read the image in Picture1 into the array named Pixels:

Private Sub Command1_Click()
    Dim x, y, intAddOn As Integer

    intAddOn = Val(Text1.Text)

    For x = 1 To intUpperBoundX
        For y = 1 To intUpperBoundY
            Pixels(x, y) = Picture1.Point(x, y)
        Next y
    Next x


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.