|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
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).
The code for this example is located in the imagecopy folder on this books accompanying CD-ROM. Now that weve seen how to work with an image bit by bit, well see how to implement some image effects in the next few topics. Creating Grayscale ImagesWeve seen how to work with images bit by bit in the previous topic. Well 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. Lets see how this works. Well 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 boxs ScaleMode property to vbPixel (3). First, we set up storage space for the image in an array named Pixels, declared in the forms (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 were free to work with the images 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 well 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, were converting an image from color to grayscale in that figure.
The code for this example is located in the imagegrayscale folder on this books accompanying CD-ROM. Lightening ImagesThe Testing Department is calling. Some of the users of your SuperDuperGraphicsPro program are saying the images in that program are too darkcan you let them lighten them? Hmm, you thinkhow does that work? You can lighten images by adding the same positive number to each color value (red, green, and blue) of each pixel. Lets see how this works in an example. Here, well 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 boxs ScaleMode property to vbPixel (3). Well 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 well 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
|
|
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. |