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


Accessing Individual Pixels In A Picture Box

The Testing Department is calling. Wouldn’t it be better to let users select new colors in your SuperDuperTextPro program by just clicking the new color they want in a picture box instead of asking them to type in new color values? Hmm, you think, how do you do that?

You can use the Point method to determine the color of a pixel in a picture box. This method returns the red, green, and blue colors in one Long integer.

Let’s see an example to make this clear. Here, we’ll let the user click one picture box, Picture1, to set the color in another, Picture2, using the MouseDown event:

Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, _
    X As Single, Y As Single)

End Sub

When the user clicks a pixel in Picture1, we’ll set the background color of Picture2 to the same color, and we get that color using the Point method:

Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, _
    X As Single, Y As Single)
    Picture2.BackColor = Picture1.Point(X, Y)
End Sub

The result of this code appears in Figure 10.12. When the user clicks a point in the top picture box, the program sets the background color of the bottom picture box to the same color.


Figure 10.12  Using the Point method to get a point’s color.


TIP:  Besides getting a pixel with the Point method, you can also set individual pixels with the PSet method. See “Drawing Lines And Circles In A Picture Box” earlier in this chapter.

Copying Pictures To And Pasting Pictures From The Clipboard

The users love your new graphics program, SuperDuperGraphics4U, but would like to export the images they create to other programs. How can you do that?

You can copy the images to the Clipboard, letting the user paste them into other programs. To place data in the Clipboard, you use SetData(), and to retrieve data from the Clipboard, you use GetData().

An example will make this clearer. Here, we’ll paste a picture from Picture1 to Picture2 using two buttons: Command1 and Command2. When users click Command1, we’ll copy the picture from Picture1 to the Clipboard; when they click Command2, we’ll paste the picture to Picture2.

To place the image in Picture1 into the Clipboard, we use SetData():

Clipboard.SetData data, [ format]

Here are the possible values for the format parameter for images:

  vbCFBitmap—2; bitmap (.bmp) file
  vbCFMetafile—3; metafile (.wmf) file
  vbCFDIB—8; device-independent bitmap (.dib) file
  vbCFPalette—9; color palette

If you omit the format parameter, Visual Basic will determine the correct format, so we’ll just copy the picture from Picture1.Picture to the Clipboard this way:

Private Sub Command1_Click()
    Clipboard.SetData Picture1.Picture
End Sub

To paste the picture, use GetData():

Clipboard.GetData ([ format])

The format parameter here is the same as for SetData(), and as before, if you don’t specify the format, Visual Basic will determine it. So when the user clicks the second button, we paste the image into Picture2 this way:

Private Sub Command2_Click()
    Picture2.Picture = Clipboard.GetData()
End Sub

That’s all it takes. When you run the program and click the Copy and then the Paste button, the image is copied to the Clipboard and then pasted into the second picture box, as shown in Figure 10.13. The program is a success. Now we’re using the Clipboard with picture boxes.


Figure 10.13  Copying a picture to and pasting it from the Clipboard.

Stretching And Flipping Images In A Picture Box

You can gain a lot more control over how images are displayed in picture boxes using the PaintPicture method:

PictureBox.PaintPicture   picture, x1, y1, [width1, height1, [x2, y2, _
    [width2, height2, [opcode]]]]

Using this method, you can stretch or flip images in a picture box. Here’s what the arguments passed to PaintPicture mean:

  picture—The source of the graphic to be drawn onto the object; should be a Picture property.
  x1, y1—Single-precision values indicating the destination coordinates (x-axis and y-axis) on the object for the picture to be drawn. The ScaleMode property of the object determines the unit of measure used.
  width1—Single-precision value indicating the destination width of the picture. The ScaleMode property of the object determines the unit of measure used. If the destination width is larger or smaller than the source width (width2), the picture is stretched or compressed to fit. If omitted, the source width is used.
  height1—Single-precision value indicating the destination height of the picture. The ScaleMode property of the object determines the unit of measure used. If the destination height is larger or smaller than the source height (height2), the picture is stretched or compressed to fit. If omitted, the source height is used.
  x2, y2—Single-precision values indicating the coordinates (x-axis and y-axis) of a clipping region within the picture. The ScaleMode property of the object determines the unit of measure used. If omitted, 0 is assumed.
  width2—Single-precision value indicating the source width of a clipping region within the picture. The ScaleMode property of the object determines the unit of measure used. If omitted, the entire source width is used.
  height2—Single-precision value indicating the source height of a clipping region within the picture. The ScaleMode property of the object determines the unit of measure used. If omitted, the entire source height is used.
  opcodeLong value or code that is used only with bitmaps. It defines a bit-wise operation (such as vbMergeCopy) that is performed on the picture as it is drawn on the object.


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.