Setting Picture and Image Properties VB Help

Another related property is tJU’ Image property, which is a pointer to n structure in memory where the bits of the image are stored, Unlike the Picture property, the Image property is read-only and used to pass bitmaps to APTfunctions, ” you’ll see in Chapter 13, The WinJoliJs API. Another difference between the Image and Picture properties is that the bitmap returned by the Picture property is the one saved along with the other Form elements in the FRX file and doesn’t include any shapes drawn on top of the bitmap at runtime. The Piclmg application, shown in Figure 6.4, demonstrates the basic difference between the two properties.

Click the Load :Picture & Dr~w button to load the Planets.bmp image on the PictureBox control in the upper-left control and draw a couple of shapes over the bitmap with Visual Basic’s drawing commands.Click the Copy Picture button to copy the contents o’f the top PlctureBox control onto the other two Picture Box controls. The bitmap is copied onto the left PictureBox control with the following command:

The same bitmap is copied 0!lto the other Picture Box with this command:

Picture3.Picture – Picture 1 .lmage’

Using the Picture property, you can copy only the bitmClf>loaded with the Load- Picture method. On the other hand, the Image property copies everything on the ” control, including the shapes. The Image property points to the persistent bitmap, which consists of the shapes drawn while the AutoRedraw property is True. Anyshapes drawn while the AutoRedrtlw property is False won’t be copied with either method. Another use of the Image property is to copy the image of a control to the Clipboard, as you’ll see in the,next section.

Exchanging Images through the Clipboard

Whether you use bitmap images or create graphics 40m scratch with the VIsual Basic dra’\4iing methods, sooner or later you’ll want to exchange them with other ,Windows’ applications. To do so, you use the Clipboard and its SetData, Get- . Datal), and GetFormat methods, which are described next. The ImgCopy application, shown in Figure 6.5, demonstrates how to use the Clipboard to exchange data with other applications. Click the Load Image button to load the Planets.bmp image on the topPictureBox control, and click the Draw on Image button to draw a couple of shapes on top of the control’s bitmap. If the AutoRedraw checkbox is checked, the A~toRedraw property of the top Picture- Box is set to True before drawing. If not, the drawing takes place on a PictureBox with its Autokedraw property set to False, and the drawing isn’t copied to the Clipboard along with the bitrnap. The other two buttons copy the first Picturebox control’s image tothe Clipboard.and paste the contents of the Clipboardon the’ second Picturebox control. The code behind the first two Command buttons is straightforward. We used similar code in the PicImg application.


Using SetData’

To copy the contents of a control to the Clipboard, you use the SetData method:

Clipboard.SetOata Picture 1 large. vbCFBitmar

The vbCFBitmap argument is a bui1t~jnconstant that corresponds to the bitmap format (one of the formats the Clipboard object can handle). The first argument of the SetData method specifies the bitmap to be copied and it can be either the Picture or the Image property of the control. If you use the Picture property, any shapes drawn.on the control atruntime won’t be copied.

The Copy to Clipboardbutton clears the Clipboard and then uses the SetData method to add data to the Clipboard.

.Whatever IS copiedto the Clipboard with the SetData method is available to my application in the Wmdows environment. For instance, if you start an image processing application after copying the contents of the first Pictureflox to the Clipboard, the application’s Paste command will be enabled, indicating that the Clipboard’s contents can be pasted orrthe current docum~t. Jotice that any shapes drawn on top of the bitmap while AutoRedraw is set to False aren’t copied to the Clipboard. For mow information in the Autokedraw property and its role in graphics,’ see the section “Optimization Issues” toward the end of this chapter.

Using GetData()

The Paste from Clipboard button uses the GetDataO method to retrieve data from the Clipboard. The code besind this button examines the contents of the Clipboard, and if the Clipboard contains an irnage, it’s copied onto the second PictureBox control.

Using GetFormat

The GetFormat method of the Clipboard object returns a True/False value, indicating whether the Clipboard’s current contents match the format specified as the parameter. In other words, the GetFormat method asks the Clipboard whether it contains a specific format, rather than what is the formatof the data it contains. Other common values for the GetFormat argument’ are the constants vbCFMetafile (Windows Metafile), vbCFPalette (Windows Palette), vbCFText (plain text), and ‘vbCFRTF (Rich Text Format). Notice how the Paste from Clipboard command uses the GetFormat method to make sure the Clipboard contains a bitmap before attempting to move it to the Picturebox.

Posted on November 5, 2015 in Drawing with Visual Basic

Share the Story

Back to Top