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


Chapter 19
Working With Images

If you need an immediate solution to:
Adding Images To Controls
Adding Images To Forms
Using Image Controls
Using Picture Boxes
AutoSizing Picture Boxes
Loading Images In At Runtime
Clearing (Erasing) Images
Storing Images In Memory Using The Picture Object
Using Arrays Of Picture Objects
Adding Picture Clip Controls To A Program
Selecting Images In A Picture Clip Control Using Coordinates
Selecting Images In A Picture Clip Control Using Rows And Columns
Flipping Images
Stretching Images
Creating Image Animation
Handling Images Bit By Bit
Creating Grayscale Images
Lightening Images
Creating “Embossed” Images
Creating “Engraved” Images
Sweeping Images
Blurring Images
Freeing Memory Used By Graphics

In Depth

Visual Basic has quite an array of techniques for dealing with images. In this chapter, we’ll work with bitmapped images in our programs, creating some powerful effects. We’ll see how to load images in, display them in a variety of ways, including flipping them and stretching them, creating image effects, and saving them back to disk.

Images can be an asset to your program, enhancing the visual interface a great deal. We won’t work on creating images here—instead, we’ll work on reading them in, working on them, and displaying them from image files on disk.

There are a number of different image formats that you use today: bitmap (.bmp), GIF, JPEG, WMF (Windows metafile format), enhanced WMF, icon (.ico), compressed bitmap (.rle), and more. Visual Basic can handle all these formats.

However, you’ll notice some anachronisms that have crept in over the years that indicate Visual Basic’s historical development—for example, the picture clip control, which we’ll see in this chapter, can only handle bitmaps with a maximum of 16 colors. This control is still a useful one, but it has largely been superseded by the more powerful image list control (which we cover in its own chapter in this book).

Picture Boxes Vs. Image Controls

The main controls that programmers use to display images are image controls and picture boxes. That’s not to say there aren’t other ways to display, of course: you can load images into many controls, like buttons, and even display them in forms, as we’ll see in this chapter. However, when programmers think of displaying and working with images, they often think of picture boxes and image controls.

It’s worth noting the difference between these controls. The image control really has one main purpose: to display images. If that’s your goal, the image control is a good choice. On the other hand, picture boxes offer you a great deal more, if you need it. You can even think of picture boxes as mini-paint programs, because they include methods to let you draw text (on top of the current image in the picture box, which is good if you want to label elements in that image), draw circles, lines, boxes, and so on.

Note, however, that the added power of picture boxes comes with an added cost in terms of heavier use of system resources. If you don’t need a picture box’s added functionality, use an image control. For more on this topic, take a look at Chapter 10.

Image Effects: Working With Images Bit By Bit

In this chapter, we’ll have some fun seeing how to work with images bit by bit. There are two main ways of doing that in Visual Basic: sticking with the Visual Basic methods, and using Windows methods directly.

We’ll stick with the Visual Basic methods, which, although slower, are vastly easier to use and get the job done well. However, you should know that we’ll take a look at the Windows way of doing things later in the book, in the chapter on connecting to Windows directly. (And you may have noticed our bitmapped menu item example in the chapter on menus works directly with Windows to create a bitmap object that it loads into a menu.)

We’ll see quite a few image effects in this chapter: embossing images, engraving images, grayscale images, image lightening, blurring images, making an image seem to sweep from upper left to lower right, and more. All these effects are powerful techniques that you might not expect from Visual Basic.

That’s it for the overview of images for the moment—it’s time to turn to the Immediate Solutions.

Immediate Solutions

Adding Images To Controls

The Aesthetic Design Department is calling again. Can’t you add some images to the controls in your program? That would make it look so much nicer.

These days, you can add images to many Visual Basic controls. For example, you can now display images in checkboxes, command buttons, and option buttons if you first set their Style property to Graphical (Style = 1), then place the name of the image file you want to use in the control’s Picture property. As an example, we display a bitmapped image in a command button in Figure 19.1.


Figure 19.1  Displaying an image in a button.

At runtime, you can load a picture into the control’s Picture property using the LoadPicture function:

Private Sub Command1_Click()
    Command1.Picture = LoadPicture("c:\image.bmp")
End Sub

Besides buttons, you can also display images in the Visual Basic image combo box—see Chapter 8. We also used a few advanced techniques to display an image in a menu item in Chapter 5.

The Windows common controls can also display images, including such controls as tree views, list views, and tab strips. There, you load the images you want into an image list control, and then connect that image list to the control using the control’s ImageList property. For more information, see Chapter 16, and the chapters on the various Windows common controls.

Adding Images To Forms

The Aesthetic Design Department is on the phone again. The form in your program looks pretty drab. How about spicing it up with an image of the company founder? Hmm, you wonder, how would you do that?

You can load an image into a form using the form’s Picture property, both at design time or at runtime. As an example, we’ve placed an image in the form you see in Figure 19.2. Note that the controls on that form are layered on top of the form’s image.


Figure 19.2  Displaying an image in a form.

At runtime, you can use the LoadPicture function to read in an image and display it in a form like this:

Private Sub Command1_Click()
    Form1.Picture = LoadPicture("c:\image.bmp")
End Sub


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.