|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Sweeping ImagesWhen we created embossed and engraved images in the previous two topics, we were careful to set up our embossing or engraving loop so that when setting a pixel, we did not make use of other pixels that we had already set in the same loop. The reason for that is that we only wanted to plot the difference between adjacent pixelsthat is, the difference between two pixels onlyto create embossed or engraved images. If we had not restricted our operation to two pixels we had not already worked on, and instead worked on pixels we had already set earlier, we could end up propagating a pixels color values among several other pixels. That is, one pixels setting could affect many other pixels. In fact, there are times when you want to have that happenfor example, you might want to make an image appear as though it is sweeping from upper-left to lower-right, giving the illusion of motion. In that case, youd copy pixels with the ones to the upper-left over and over, progressively blending them together to create the effect you see in Figure 19.16, where it looks as though the text has a fading trail of color behind it.
Seeing the code that gives us the image in Figure 19.16 will help make this effect easier to understand. As with the previous few topics in this chapter, we load the image in a picture box, Picture1, into an array named Pixels. Then we move from lower-right to upper-left, averaging each pixel with the one to the lower-right:
For x = intUpperBoundX 1 To 1 Step 1
For y = intUpperBoundY 1 To 1 Step 1
bytRed = Abs((Pixels(x + 1, y + 1) And &HFF) + (Pixels(x, y)_
And &HFF)) / 2
bytGreen = Abs(((Pixels(x + 1, y + 1) And &HFF00) / &H100)_
Mod &H100 + ((Pixels(x, y) And &HFF00) / &H100) Mod_
&H100) / 2
bytBlue = Abs(((Pixels(x + 1, y + 1) And &HFF0000) / &H10000) _
Mod &H100 + ((Pixels(x, y) And &HFF0000) / &H10000) Mod_
&H100) / 2
Pixels(x, y) = RGB(bytRed, bytGreen, bytBlue)
Next y
Next x
Thats all it takesnow we copy the image into the second picture box, Picture2. (To be able to work pixel by pixel, make sure you set each picture boxs ScaleMode property to vbPixel (3).) By combining successive pixels as we do in this example, we create the sweeping effect you see in Figure 19.16. Now were creating complex images using image handling techniques. The complete code for this example is located in the imagesweep folder on this books accompanying CD-ROM. Blurring ImagesThe Aesthetic Design Department is calling again. If youre going to add image effects to your program, SuperDuperGraphicsPro, why not let the user blur images? You can blur images by averaging pixels. To see how this works, we load the pixels from a picture box, Picture1, and blur them, then display the result in another picture box, Picture2. To be able to work pixel by pixel, set each picture boxs ScaleMode property to vbPixel (3). As with the code in the previous few topics in this chapter, we load the pixels from Picture1 into an array named Pixels. To blur the pixels, you average them together; here, we just average each pixel with the next pixel to the right , but you can set up any blurring region you like (such as all eight pixels that surround the current pixel). This is the way our blurring process looks in code:
For x = 1 To intUpperBoundX 1
For y = 1 To intUpperBoundY
bytRed = Abs((Pixels(x + 1, y) And &HFF) + (Pixels(x, y) _
And &HFF)) / 2
bytGreen = Abs(((Pixels(x + 1, y) And &HFF00) / &H100) Mod _
&H100 + ((Pixels(x, y) And &HFF00) / &H100) Mod &H100) / 2
bytBlue = Abs(((Pixels(x + 1, y) And &HFF0000) / &H10000) _
Mod &H100 + ((Pixels(x, y) And &HFF0000) / &H10000)_
Mod &H100) / 2
Pixels(x, y) = RGB(bytRed, bytGreen, bytBlue)
Next y
Next x
After the pixels have been blurred, we display the result in Picture2, as shown in Figure 19.17. As you can see, the blurring produced with this algorithm is slight; to blur the image more, you can apply the same algorithm again or increase the number of pixels over which you average.
The code for this example is located in the imageblur folder on this books accompanying CD-ROM. Freeing Memory Used By GraphicsThe Testing Department is calling again. Your program, SuperDuperGraphicsPro, is using up a lot of memory. Is there any way to free some memory when youre not using it anymore? Yes, there is. When you are no longer using a picture in the Picture property of a form, picture box, or image control, set the Picture property to the Visual Basic Nothing keyword to empty it: Set Picture1.Picture = Nothing In addition, if you use the Image property of a picture box or form, Visual Basic creates an AutoRedraw bitmap (this happens even if the AutoRedraw property for that form or picture box is False). When youve finished using the Image property, you can empty the memory used by that bitmap with the Cls method before you set AutoRedraw to False, as in this example: Picture1.AutoRedraw = True Picture1.Cls Picture1.AutoRedraw = False
|
|
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. |