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


Then we could use pixel dimensions in the MouseDown event:

Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As _
    Single, Y As Single)
    If X > 16 And X < 83 And Y > 11 And Y < 36 Then
        MsgBox "You clicked the word ""Picture"""
    End If

    If X > 83 And X < 125 And Y > 11 And Y < 36 Then
        MsgBox "You clicked the word ""Box"""
    End If
End Sub

If you set the scale mode to vbUser, you can define your own units by setting the dimensions of the picture box using the ScaleLeft, ScaleTop, ScaleWidth, and ScaleHeight properties. This can be very useful if you’re plotting points and want to use a picture box as a graph.


TIP:  The ScaleWidth and ScaleHeight properties of a picture box hold the image’s actual dimensions (in units determined by the ScaleMode property), not the Width and Height properties, which hold the control’s width and height (including the border).

Saving Pictures To Disk

We already know you can load pictures into a picture box with the LoadPicture function. Can you save them to disk?

Yes, you can, using SavePicture. Here’s how that statement works:

SavePicture   picture, stringexpression

Here’s what the parameters for SavePicture mean:

  picture—Picture or image control from which the graphics file is to be created
  stringexpression—File name of the graphics file to save

SavePicture only saves images in BMP, WMF, and ICO formats (depending on the file type the image came from originally); if the image came from a GIF or JPEG file, it’s saved in BMP format. Graphics in an Image property are always saved as bitmap (.bmp) files no matter what their original format.

Here’s an example where we save the image from Picture1 to a file, C:\image.bmp, when the user clicks a button:

Private Sub Command1_Click()
    SavePicture Picture1.Picture, "c:\image.bmp"
End Sub

Adding An Image Control To A Form

You’ve got 200 picture boxes in your program, and suddenly the Testing Department is on the line: your program is causing users’ computers to run out of memory. No problem here, you say. They say, that’s because not everyone has 128MB of RAM like you do—it’s time to decrease your program’s memory consumption.

One way of using fewer system resources is to use fewer picture boxes. As we’ve seen in this chapter, picture boxes are powerful controls—and with that power comes lots of overhead. If you’re just going to be displaying images, use image controls instead. The image control uses fewer system resources and repaints faster than a picture box (however, it supports only a subset of the picture box properties, events, and methods).

To install an image control, just use the Image Control tool in the toolbox. After adding the image control to your form, just set its Picture property to the image file you want to display. By default, image controls shape themselves to the image you display; if you want to stretch the image to fit the image control and not the other way around, set the image control’s Stretch property to True (the default is False).

As an example, we’ve placed an (unstretched) image in the image control in Figure 10.15.


Figure 10.15  Using an image control.

Stretching An Image In An Image Control

You can stretch (or flip) an image in a picture box using the PaintPicture method, but you can’t use PaintPicture with image controls. Is there still some way of producing interesting graphics effects in an image control?

You can use the image control’s Stretch property. By default, image controls shape themselves to fit the images inside them (after all, their primary purpose is to display images), but if you set the Stretch property to True (the default is False), the image control will stretch the image to fit the control.

As an example, we’re stretching an image in the image control in Figure 10.16.


Figure 10.16  Stretching an image in an image control.

You can also stretch an image in an image control by resizing the control (using its Width and Height properties) at runtime as long as the control’s Stretch property is True.


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.