Search In Site

01 July, 2013

How to Create Grayscale And lightening Images in Vb


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

Lightening Images
The Testing Department is calling. Some of the users of your  super Duper Graphics Pro 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
Next, we get the red, green, and blue color values for each pixel and add the value in intAddOn to those color values, making sure they don’t go higher than 255 (of course, you can also darken images by subtracting values here, although you should make sure the resulting color values don’t go below 0):
Private Sub Command1_Click()
Dim x, y, intAddOn As Integer
Dim bytRed, bytGreen, bytBlue 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
bytRed = bytRed + intAddOn
If bytRed > 255 Then bytRed = 255
bytGreen = bytGreen + intAddOn
If bytGreen > 255 Then bytGreen = 255
bytBlue = bytBlue + intAddOn
If bytBlue > 255 Then bytBlue = 255
Pixels(x, y) = RGB(bytRed, bytGreen, bytBlue)
Next y
Next x
End Sub
Finally, we just copy the new pixels to the second picture box, Picture2:
Private Sub Command1_Click()
Dim x, y, intAddOn As Integer

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

0 comments:

Post a Comment

Dear Visitors All The Tricks And Hacks Posted Here Are Only For Knowledge Purpose.Don't Use These for Illegal Operations.

 
Twitter Bird Gadget