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


Let’s see an example to show how the Picture object works. First, we create a Picture object, picObject1:

Private Sub Command1_Click()
    Dim picObject1 As Picture
…
End Sub

Then we load the image into that Picture object using LoadPicture:

Private Sub Command1_Click()
    Dim picObject1 As Picture
    Set picObject1 = LoadPicture("c:\image.bmp")
…
End Sub

Finally, we just set a picture box’s Picture property to the Picture object, and that’s it:

Private Sub Command1_Click()
    Dim picObject1 As Picture
    Set picObject1 = LoadPicture("c:\image.bmp")
    Set Picture1.Picture = picObject1
End Sub

You can also use the Render method to draw images with the Picture object (although PaintPicture is Microsoft’s preferred method these days).

The Render Method

Here’s how you use the Render method to draw images with the Picture object:

PictureObject.Render (hdc, xdest, ydest, destwid, desthgt, xsrc, ysrc, _
    srcwid, srchgt, wbounds)

Here are what the arguments for Render mean:

  hdc—The handle to the destination object’s device context, such as Picture1.hDC.
  xdest—The x-coordinate of the upper-left corner of the drawing region in the destination object. This coordinate is in the scale units of the destination object.
  ydest—The y-coordinate of the upper-left corner of the drawing region in the destination object. This coordinate is in the scale units of the destination object.
  destwid—The width of the drawing region in the destination object, expressed in the scale units of the destination object.
  desthgt—The height of the drawing region in the destination object, expressed in the scale units of the destination object.
  xsrc—The x-coordinate of the upper-left corner of the drawing region in the source object. This coordinate is in HiMetric units.
  ysrc—The y-coordinate of the upper-left corner of the drawing region in the source object. This coordinate is in HiMetric units.
  srcwid—The width of the drawing region in the source object, expressed in HiMetric units.
  srchgt—The height of the drawing region in the source object, expressed in HiMetric units.
  wbounds—The bounds of a metafile. This argument should be passed a value of Null unless drawing to a metafile, in which case the argument is passed a user-defined type corresponding to a RECTL structure.


TIP:  Note that some of the arguments to Render must be in HiMetric units. Here’s an important note: You can convert from one set of units to another using the Visual Basic ScaleX and ScaleY functions, so use those functions to convert from twips or pixels to HiMetric.

Using Arrays Of Picture Objects

You can use an array of Picture objects to keep a series of graphics in memory without using a form that contains multiple picture box or image controls. This is good for creating animation sequences or other applications where rapid image changes are required.

Let’s see an example. Here, we’ll create an array of Picture objects and load images into them. We start by setting up an array of two Picture objects as a form-wide array:

Dim picObjects(1 To 2) As Picture

Then when the form loads, we read in two image files into the array:

Private Sub Form_Load()
    Set picObjects(1) = LoadPicture("c:\vbbb\pictureanimation\image1.bmp")
    Set picObjects(2) = LoadPicture("c:\vbbb\pictureanimation\image2.bmp")
End Sub

Now the images in the array will be available for use in our program (and we’ll use them in a later topic in this chapter—see “Creating Image Animation”).

Adding Picture Clip Controls To A Program

One way of storing images in a Visual Basic program is to use a picture clip control. This control stores a number of images as one large bitmap, and to get the image you want, you have to clip it out of that bitmap. If that sounds a little less convenient to you than using an image list control or array of Picture objects, you’re right—it is. Picture clips were first made available long ago in Visual Basic and don’t support all the convenience of more modern controls. However, programmers still use them, and we’ll cover them here.


TIP:  One excellent reason to use picture clip controls besides storing images is to edit existing images, because picture clip controls let you clip rectangular sections of image from exiting images.

To add a picture clip control to a program, follow these steps:

1.  Select the Project|Components menu item.
2.  Click the Controls tab in the Components dialog box.
3.  Select the Microsoft PictureClip Control item.
4.  Close the Components dialog box by clicking on OK.
5.  This adds the Picture Clip Control’s tool to the Visual Basic toolbox; that tool is at the bottom right in Figure 19.4. Use that tool to draw a picture clip control in your program; because the control is invisible at runtime, size and placement of the control don’t matter.


Figure 19.4  The Picture Clip Control tool.

Now we add an image that consists of three images added together to the picture clip control, as you can see in Figure 19.5. When you want to get a picture from a picture clip control, you specify the (x, y) coordinates of the bitmap section you want, and its height and width. You can also divide the image up into rows and columns, as we’ll see in a few topics.


Figure 19.5  Adding a picture clip control to a program.

To put the picture clip control to work, see the next few topics in this chapter.

Selecting Images In A Picture Clip Control Using Coordinates

You’ve placed all the images you want to store into a picture clip control as one large bitmap. How can you get your images back out again?

There are two ways to get images out of a picture clip control (three, if you count accessing the whole bitmap with the control’s Picture property): by specifying the image’s coordinates in the whole bitmap, or by breaking the bitmap into rows and columns and accessing the image by row and column. After you specify an image, you can retrieve it using the picture clip’s Clip property. We’ll see how to use bitmap coordinates in this topic, and rows and columns in the next topic.


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.