VB6 at Work: The PaintPic Project

The PaintPic application, shown in Figure 7.S, lets you combine the pixels of the source (left) and destination (right) PictweBox controls with the various values of the RRsterop argument of the PaintPicture method. The eight sliders let you set the origin of the rectangle to be copied and its dimensions, the destination coordinates of the transfer, and the transfer’s dimensions on the deStination PictweBox control. You can copy the entire source image, or part of it, and place it anywhere on the destination control. You can also specify different dimensions on the two controls to see how the PaintPicture method distorts the image’s dimensions during the transfer. Also, try the various settings for the raster operator with different images.

Notice that since most raster operations combine the source with the deStination pixels, the result of.the transfer depends on the destination control’ current contents. The vbSrclnvert operation, for instance, merges the pixels of the IOUI’C.’e image with the destination pixels using the XOR operator. If you copy the source image again with the same setting, you’ll get the original contents of the destination image. That’s because the XOR operator works like a toggle that merges the two images and then restores aw’originaI. The first time, it’s actually encrypting the original image; the second time, it reveals it. You can also experiment ·th successive transfers of the same image with different se#ings for the raster operation. Some settings will yield interesting effects; especially if y.ou change each successive image’s destination by one or two pixels. Finally, you can set the destination PictureBox control’s background color to black or white by checking the Blackness OJ’ Whiteness checkbox. You can also reset the destion PictureBox control’s bad” round  olor by right-clicldng it.

Flipping an Image with PaintPicture

Another interesting application of the PaintPicture method is to flip an image as it copies it. If the image’s width is negative, the image flips horizontally; if the image’s height is negative, the image flips vertically. If.both width and height are negative, the image flips in both directions. The negative sign in the width of the destination, for example. tells the PaintPicture method to copy the pixels to the left of the origin (the same is true for the height of the destination). Thus, the pixels are copied to the left of the destination control’s left edge. The entire image is copied outside the destination control. To compensate, set the origin to the other comer of the destination. Figure· 7.6 shows the PicFlip application, which uses the PaintPicture method to flip an image as it-copies it.

The Copy button transfers the source imege to the destination. Let’s look at the code of the Copy button in-the PicFlip application next.

Compare the.Copy button code with the code of the Flip HorlzO”ntalbutton shown next

Notice that in order to flip the image horizontally, the destination’s X origin is not but the actual image width. The destination’s width is the negative of the actual width. The code behind the Flip Vertical button is similar, but instead of the X coordinate and the width of the image, it inverts the Y coordinate and the height of the image. The Flip Both button inverts both the X and Y coordinates and the width and height of the destination.

Processing Images

Images are arrays of pixels, much like a PictureBox control. The values of the image’s pixels are stored in disk files and when an image is displayed on a Picture- Box or Form control, each one of its pixels is mapped to a pixel on the PictureBox or Form. As you’ll see, image processing is nothing more than simple arithmetic operations on the values of the image’s pixels. The Image application we’ll build to demonstrate the various image processing techniques is slow compared with professional applieations, but it demonstrates the principles of image processing techniques that can be used as a starting point for custom applications. We’ll build a simple image processing application that can read BMP,GIF, and JPEG image files, process them, and then display the processed images. There are simpler ways to demonstrate Visual Basic pixel-handling methods, but image processing is an intriguing topic, and I hope many readers will experiment with its techniques in the Image application. In Chapter 12, we’ll optimize the Image application and make it considerablr faster.

An image is a two-dimensional array of pixels in which each pixel is represented by oneor more bits. In a black-and-white image, each pixel is represented by a single bit. In the most common types of images, or those with 256 colors, each pixel is represented by a byte. The best quality images, however, use three bytes per pixel, one for each RGB color component. For a discussion of the variousmethods of storing and displaying colors, see the sidebar “True Color and Palette Systems,” earlier.

Images composed of 256 colors are based on a palette; that’s why t!’ey’re sometimes called palette images. The program that creates the image selects the 256 colors that best describe the image and stores them in a palette, along with the image. Each byte in the image is a pointer to the pixel’s color in the palette. If the· original image happens to.contain more than 256 colors, some of them must be approximated with the existing colors. The best approach for processing this image would be to create a new palette that best describes the new image, but – this topic is beyond the scope of this book. Palette manipulation is a complicated topic and requires extensive use of API functions. A total of 256 colors is sufficient for describing a typical image: Even if some of the colors must be approximated, the quality of the image is relatively good. Most pages on the World Wide Web contain no more than 256 colors and they look fine.
• The problem with palettes isn’t with the image, but with the computers that display them. If your CO!Ilputcrcan display True Color, it can display any number of images, even when opened at the same time. But systems that can display only 256 colors at a time will have a problem. The palette system won’t be able to correctly display the new colors introduced by the processing algorithm (see the sidebar “True Color and Palette Systems,” earlier in this chapter). For example, if the original image contains 256 colors, the processing algorithm will most likely introduce additional colors that aren’t present in the palette. Because the computer can’t display more than 256 colors, some of them must be approximated. The Image application works best on True Color systems. It will also work on systems capable of displaying palette images, but some of the colors may not look quite right.
Because images are two-dimensional arrays of integers, their processing is nothing more than simple arithmetic operations on these integers. Let’s look at a simple technique, the inversion of all image’s colors. To invert an image, you must change all pixels to their complementary colors-black to white, green to magenta, and so on (the complementary colors are on opposite comers of the RGB cube, shown in Figure 7,1, earlier in this chapter).

mid-yellow tone (0, 128, 128) will be converted to (255-0, 255-128, 255-128) or (255,127,127), which is a mid-brown tone. To invert an image’s colors, you set up two loops that scan the image’s pixels and invert the colors of the pixels. The result is the negative of the original image (what you’d see if you looked at the negative from which the picture was obtained). Other image processing techniques aren’t as simple, but the important thing to understand is that, in general, image processing is as straightforward as a few arithmetic operations on the image’s pixels. After we go through the Image application, you’ll probably come up with your own techniques and be able to implementthem.”

VB6 at Work: The Image Project

The application we’ll develop in this section is called Image and it’s shown in Figure 7.7. It’s not a professional tool, but it can be easily implemented in Visual Basic, and it will give you the opportunity to explore various image processing techniques on your own. To process an image with the Image application, choose File> Open to load it to the PictureBox control and then select the type of processing from the Process menu. Using the Image application, you can apply the following effects to an iMage

• Smooth            Reduces the amount of detail
•  Sharpen          Brings out the detail
• Emboss            Adds a raised (embossed) look
• Diffuse              Gives a painterly look
• Solarize           Creates a special effect based on selective pixel                                inversion
• Custom Filter  Allows the user to specify the effect to be applied                               to the image


Next, let’s look at how each algorithm works and how it’s implemented in Visual Basic

Scroll to Top