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 10
Picture Boxes And Image Controls

If you need an immediate solution to:
Adding A Picture Box To A Form
Setting Or Getting The Picture In A Picture Box
Adjusting Picture Box Size To Contents
Aligning A Picture Box In A Form
Handling Picture Box Events (And Creating Image Maps)
Picture Box Animation
Grouping Other Controls In A Picture Box
Using A Picture Box In An MDI Form
Drawing Lines And Circles In A Picture Box
Using Image Lists With Picture Boxes
Adding Text To A Picture Box
Formatting Text In A Picture Box
Clearing A Picture Box
Accessing Individual Pixels In A Picture Box
Copying Pictures To And Pasting Pictures From The Clipboard
Stretching And Flipping Images In A Picture Box
Printing A Picture
Using Picture Box Handles
Setting Measurement Scales In A Picture Box
Saving Pictures To Disk
Adding An Image Control To A Form
Stretching An Image In An Image Control

In Depth

In this chapter, we’re going to take an in-depth look at two popular Visual Basic controls: image controls and picture boxes. In fact, this will be our introduction to a very popular Visual Basic topic, working with graphics, because picture boxes let you do far more with images than just display them.

The two controls we’ll work with in this chapter appear in Figure 10.1. We’ll take a closer look at these two controls now.


Figure 10.1  A picture box and an image control.

Image Controls

You use image controls to do just what the name implies: display images. This control is a very simple one that doesn’t take up many program resources: it’s just there to display (and stretch, if you wish) images. If that’s all you want to do, use an image control. You load a picture into an image control’s Picture property (either at design time or using LoadPicture() at runtime).


TIP:  You should also know that if you just want to display a picture as a sort of backdrop for your program, Form objects themselves have a Picture property that you can load images into without the need for image controls or picture boxes.

Image controls are very accommodating—they resize themselves automatically to fit the image you’re placing in them. On the other hand, if you don’t want the image control to change size, set its Stretch property to True. Doing so means that the image, not the control, will be resized when loaded to fit the control. Another advantage of the image control over the picture box is that it repaints itself faster than picture boxes. Image boxes can’t do a lot of things that picture boxes can do, however, such as act as containers for other controls.

Both image controls and picture boxes are intrinsic controls in Visual Basic, which means they appear in the toolbox when you start the program. The Image Control tool is tenth down on the left in the toolbox in Figure 10.2.


Figure 10.2  The Image Control tool.

Picture Boxes

Picture boxes are more complete controls than image controls. Just as the rich text control provides a sort of word-processor-in-a-control, so the picture box does for graphics in Visual Basic. You can load images into a picture box, save images to disk, draw with some rudimentary graphics methods, print images, work pixel-by-pixel, set an image’s scale, and more. Besides graphics handling, the picture box can also act as a container for other controls—and besides toolbars and status bars, it’s the only control that can appear by itself in an MDI form.

As with image controls, you load pictures into a picture box’s Picture property, and you can do that at design time or runtime (at runtime you use the LoadPicture() method). When you load an image into a picture box, the picture box does not resize itself by default to fit that image as the image control does—but it will if you set its AutoSize property to True. The picture box has a 3D border by default, so it doesn’t look like an image control—unless you set its BorderStyle property to 0 for no border (instead of 1, the default). In other words, you can make a picture box look and behave just like an image control if you wish, but keep in mind that picture boxes use a lot more memory and processor time, so if you just want to display an image, stick with image controls.

Like image controls, picture boxes are intrinsic controls in Visual Basic; the Picture Box tool is at right in the first row of tools in Figure 10.3.


Figure 10.3  The Picture Box tool.

That’s all the overview we need for these two popular controls. It’s time to start working with them directly in the Immediate Solutions.

Immediate Solutions

Adding A Picture Box To A Form

You’ve decided that you need a picture box in your program. How do you add one? Adding a picture box is simple; just follow these steps:

1.  Select the Picture Box tool in the toolbox, and double-click it to add a picture box to your form, or click it once and draw the picture box where you want it on the form.
2.  If you want the picture box to resize itself to fit the picture you’ll load into it, set its AutoSize property to True. If you don’t want a border on the control, set its BorderStyle property to None (0).
3.  If you want the picture box’s contents to be refreshed when needed (for example, in case another window obscuring the picture box is removed), set its AutoRedraw property to True.
4.  Load the image you want to display into the picture box using its Picture property. Click that property in the Properties window and click the button with an ellipsis (...) in it to open the Load Picture dialog box. At runtime, you can load a picture using LoadPicture() like this:
Private Sub Command1_Click()
    Picture1.Picture = LoadPicture _
        ("c:\vbbb\picturesandimages\image.bmp")
End Sub

We’ve loaded an image into the picture box in Figure 10.4 following the preceding steps. Now the picture box is ready to go. That’s all there is to it.


Figure 10.4  A picture box in a form.

Setting Or Getting The Picture In A Picture Box

You’ve added a new picture box to your form, and it looks fine—except for one thing: it’s completely blank. How do you add images to a picture box again?

You use the Picture property. A picture box is very versatile and can display images from bitmap (.bmp), icon (.ico), metafile (.wmf), JPEG (.jpg), or GIF (.gif) files—just load the file’s name into the Picture property.


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.