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


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

The result of this code appears in Figure 19.13. Now we’re lightening images pixel by pixel in Visual Basic.


Figure 19.13  Lightening an image pixel by pixel.

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

Creating “Embossed” Images

You can create a striking visual effect by embossing an image, which makes it appear to be raised in 3D. Using the technique developed in the previous few topics, we can work pixel by pixel in an image to emboss it.

Let’s see how this works in an example. Here, we’ll take the image in a picture box, Picture1, and emboss the image in it when the user clicks a 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 start by storing the image in Picture1 in an array named Pixels:

Const intUpperBoundX = 300
Const intUpperBoundY = 300
Dim Pixels(1 To intUpperBoundX, 1 To intUpperBoundY) As Long
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’ll emboss the image in the Pixels array. Embossing is the process of plotting the difference between a pixel and a pixel above and to the left of it; this difference is added to 128 to make the whole image appear gray. Here’s one important note: when we’re setting a pixel, we use both it and the pixel to the upper-left of it, which means that to avoid incorporating pixels we’ve already set, we will proceed from the bottom-right of the array, not the upper-left. Here’s how that process looks in code:

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

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

End Sub

Note that we also average all the color values together so that the resulting image is a grayscale image. When we’re done, we just copy the image to the second picture box, Picture2:

Private Sub Command1_Click()
    Dim bytRed, bytGreen, bytBlue, bytAverage As Integer
…
    For x = 1 To intUpperBoundX
        For y = 1 To intUpperBoundY
            Picture2.PSet (x – 2, y – 2), Pixels(x, y)
        Next y
    Next x

End Sub

Running this program gives the result you see in Figure 19.14. Now we’re embossing images in Visual Basic.


Figure 19.14  Embossing an image pixel by pixel.

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

Creating “Engraved” Images

In the previous topic, we created embossed images by taking the difference between a pixel and the pixel to the upper-left of it and adding 128 to the result to create a grayscale image. We can also create engraved images by taking the difference between a pixel and the pixel to its lower-right and adding 128 to the result.

The code to create engraved images is the same as that to create embossed images, except that we work in the reverse direction and use the pixel to the lower-right, not the upper-left. Here’s the new image effect loop:

    For x = 2 To intUpperBoundX – 1
        For y = 2 To intUpperBoundY – 1
              bytRed = ((Pixels(x + 1, y + 1) And &HFF) – (Pixels(x, y) And _
                &HFF)) + 128
            bytGreen = (((Pixels(x + 1, y + 1) And &HFF00) / &H100) _
                   Mod &H100 – ((Pixels(x, y) And &HFF00) / &H100) Mod &H100)_
                + 128
            bytBlue = (((Pixels(x + 1, y + 1) And &HFF0000) / &H10000)_
                Mod &H100 – ((Pixels(x, y) And &HFF0000) / &H10000) Mod_
                &H100) + 128

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

When you put this code to work, you see the result as in Figure 19.15. Now we’re engraving images in Visual Basic. (To be able to work pixel by pixel, make sure you set each picture box’s ScaleMode property to vbPixel (3).)


Figure 19.15  Engraving images by working pixel by pixel.

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


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.