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


For example, here’s how we stretch an image to fill a picture box (here, the picture we’re stretching is the picture that already is displayed in the picture box—we’re just sizing it to fill the picture box by making its width and height the width and height of the picture box):

Private Sub Form_Load()
    Picture1.PaintPicture Picture1.Picture, 0, 0, Picture1.ScaleWidth,_
        Picture1.ScaleHeight
End Sub

In Figure 19.9, we’re applying this code to the picture in the picture box.


Figure 19.9  Stretching an image in an image control.

What About Image Controls?

You can stretch (or flip) an image in a picture box, form, or the Printer object 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 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 19.9.

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. The code for the example is located in the imagestretch folder on this book’s accompanying CD-ROM.

Creating Image Animation

One way to create image animation is to use a picture box and keep changing its Picture property to display successive frames of an animation. You can store the images themselves in the program, such as using an image list control or an array of Picture objects. We’ve seen how to create animation earlier in this book in our chapter on Visual Basic timers using image lists; here, we can do the same thing using an array of Picture objects.

We add a timer control, Timer1, to the program and set its Interval property to 1000 (the Interval property is measured in milliseconds, 1/1000s of a second), which means the Timer1_Timer() event handler will be called once a second. We also add a picture box, Picture1, in which to display images and a command button, Command1, with the caption “Start animation” to start the animation.

For the purposes of this example, we will just switch back and forth between two images in the picture box. These two images are the two images in the Picture object array, picObjects, which we store in the form’s (General) section:

Dim picObjects(1 To 2) As Picture

We load those images when the form first loads:

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

To switch back and forth, we use a static Boolean flag named blnImage1 like this, alternating between images in the Picture object array in Timer1_Timer:

Private Sub Timer1_Timer()
    Static blnImage1 As Boolean

     If blnImage1 Then
        Picture1.Picture = picObjects(1)
    Else
        Picture1.Picture = picObjects(2)
    End If
…

At the end of Timer1_Timer, we toggle the blnImage1 flag this way:

Private Sub Timer1_Timer()
    Static blnImage1 As Boolean
    If blnImage1 Then
        Picture1.Picture = picObjects(1)
    Else
        Picture1.Picture = picObjects(2)
    End If

    blnImage1 = Not blnImage1
End Sub

All that’s left is to start the animation when the user clicks the command button, and we do that like this, by enabling the timer:

Private Sub Command1_Click()
    Timer1.Enabled = True
End Sub

And that’s all we need—now we’re supporting animation using picture boxes and Picture object arrays. The result of this code appears in Figure 19.10.


Figure 19.10  Image animation with a picture box.

The code for this example is located in the pictureanimation folder on this book’s accompanying CD-ROM.

Handling Images Bit By Bit

The Aesthetic Design Department is calling again. How about adding some special effects to the images in your program, SuperDuperGraphicsPro? Doesn’t that mean working with images bit by bit, you ask? Probably, they say.

We can use Visual Basic methods to work bit by bit with images. Does that mean we’ll actually use the PSet (sets a pixel) and Point (reads a pixel) methods to handle whole images? Exactly.

We’ll see this in action over the next few topics. In this topic, we’ll see how to read an image in from one picture box, Picture1, and write it out to another, Picture2, when the user clicks a command button, Command1. To be able to work pixel by pixel, set each picture box’s ScaleMode property to vbPixel (3).

We start by setting up an array, Pixels, to hold the colors of each pixel for an image up to 500 × 500 (to be more efficient, you can redimension your storage array with ReDim when you know the actual size of the image you’re to work with) in the (General) declarations area of the form:

Const intUpperBoundX = 500
Const intUpperBoundY = 500
Dim Pixels(1 To intUpperBoundX, 1 To intUpperBoundY) As Long

The first task is to read the pixels in from Picture1, and we start by setting up loops over all the pixels in that image:

Private Sub Command1_Click()
    Dim x, y As Integer

     For x = 1 To intUpperBoundX
        For y = 1 To intUpperBoundY
…
        Next y
    Next x

Then we read each pixel from Picture1 using the Point method and store the pixels in the Pixels array we’ve set up:

Private Sub Command1_Click()
    Dim x, y As Integer

    For x = 1 To intUpperBoundX
        For y = 1 To intUpperBoundY
            Pixels(x, y) = Picture1.Point(x, y)
        Next y
    Next x

Now we’ve stored the image in the Pixels array. To copy that image to Picture2, we just use that control’s PSet method, pixel by pixel:

Private Sub Command1_Click()
    Dim x, y As Integer

    For x = 1 To intUpperBoundX
        For y = 1 To intUpperBoundY
            Pixels(x, y) = Picture1.Point(x, y)
        Next y
    Next x

    For x = 1 To intUpperBoundX
        For y = 1 To intUpperBoundY
            Picture2.PSet (x, y), Pixels(x, y)
        Next y
    Next x

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.