617 621




Visual Basic 6 Black Book:Working With Images
function GetCookie (name) { var arg = name + "="; var alen = arg.length; var clen = document.cookie.length; var i = 0; while (i < clen) { var j = i + alen; if (document.cookie.substring(i, j) == arg) { var end = document.cookie.indexOf (";", j); if (end == -1) end = document.cookie.length; return unescape(document.cookie.substring(j, end)); } i = document.cookie.indexOf(" ", i) + 1; if (i == 0) break; } return null; } var m1=''; var gifstr=GetCookie("UsrType"); if((gifstr!=0 ) && (gifstr!=null)) { m2=gifstr; } document.write(m1+m2+m3);            Keyword Title Author ISBN Publisher Imprint Brief Full  Advanced      Search  Search Tips Please Select ----------- Components Content Mgt Certification Databases Enterprise Mgt Fun/Games Groupware Hardware IBM Redbooks Intranet Dev Middleware Multimedia Networks OS Prod Apps Programming Security UI Web Services Webmaster Y2K ----------- New Titles ----------- Free Archive 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 function isIE4() { return( navigator.appName.indexOf("Microsoft") != -1 && (navigator.appVersion.charAt(0)=='4') ); } function bookMarkit() { var url="http://www.itknowledge.com/PSUser/EWBookMarks.html?url="+window.location+"&isbn=0"; parent.location.href=url; //var win = window.open(url,"myitk"); //if(!isIE4()) // win.focus(); } Search this book:  














Previous
Table of Contents
Next




The code for the example you see in Figure 19.2 is located in the imageform folder on this book’s accompanying CD-ROM.


TIP:  Note that if you just want to set the background color of a form to some uniform color, you should use the form’s BackColor property instead of loading an image in.

Using Image Controls
You use image controls to display images. Although that might seem obvious, it’s usually the deciding factor in whether or not to use an image control or a picture box. Image controls are simple controls that don’t use many system resources, whereas picture boxes are more powerful controls that do. When you just have an image to display, this is the control to use.

You load an image into an image control using its Picture property at design time or runtime. When you load an image in at runtime, use the LoadPicture function this way:


Private Sub Command1_Click()
Image1.Picture = LoadPicture("c:\image.bmp")
End Sub


As you can see in the image control in Figure 19.3, image controls have no border by default, although you can add one using the BorderStyle property. In addition, image controls size themselves to the image they display automatically, unless you set their Stretch property to True, in which case they size the image to fit themselves.

Figure 19.3  An image control and a picture box.
Image controls support events like Click, DblClick, MouseDown, MouseMove, and MouseUp. However, they do not support all the events that picture boxes support, such as Key events. In general, you use image controls for one purpose only: to display an image (which can include stretching that image). Both image controls and picture boxes can read in images in all the popular formats: GIF, JPEG, BMP, and so on.
For a lot more information on image controls, take a look at Chapter 10.
Using Picture Boxes
Picture boxes are like mini-paint programs. Not only can they display images—they can also create or modify them. You can use the built-in methods of picture boxes to draw text, ellipses, lines, boxes, and more, on top of the images they display.

You load an image into a picture box using its Picture property at design time or runtime. When you load an image in at runtime, use the LoadPicture function this way:


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


As you can see in Figure 19.3, picture boxes display a border by default, although you can remove it with the control’s BorderStyle property. By default, picture boxes display their images starting at the picture box’s upper-left corner (leaving uncovered space at the lower-right blank), but you can change that by setting the AutoSize property to True. When you set AutoSize to True, the picture box sizes itself to fit its displayed image.
You can use a picture box’s PaintPicture method to draw an image at different locations in a picture box, and even flip it as we’ll see in this chapter. Both image controls and picture boxes can read in images in all the popular formats: GIF, JPEG, BMP, and so on.
For a lot more information on picture boxes, take a look at Chapter 10.
AutoSizing Picture Boxes
Image controls size themselves automatically to fit the image they’re displaying—but picture boxes don’t, by default. You can, however, make them resize themselves to fit the image they’re displaying by setting the picture box’s AutoSize property to True. You can set AutoSize to True either at design time or at runtime.
Loading Images In At Runtime
You know that you use the Picture property to load images into image controls and picture boxes, but how does that work at runtime? This code doesn’t seem to work:


Private Sub Command1_Click()
Image1.Picture = "c:\image.bmp" 'Error!
End Sub


You have to use the Visual Basic LoadPicture function here. That looks like this when we load an image into an image control:


Private Sub Command1_Click()
Image1.Picture = LoadPicture("c:\image.bmp")
End Sub


Here’s how we load that image into a picture box:



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


You can also load an image into a Visual Basic Picture object. Let’s see an example of how that works. First, we create a Picture object, picObject1:


Private Sub Command1_Click()
Dim picObject1 As Picture

End Sub


Next, 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


If, on the other hand, you want to save an image to disk, use the picture box’s SavePicture method.
Clearing (Erasing) Images
One of the handiest things to know about handling images is how to clear an image in a form or picture box. You use the Cls method (which originally stood for “Clear Screen”) to do that (image controls don’t have a Cls method).
For example, here’s how we erase an image in a picture box when the user clicks that picture box:


Private Sub Picture1_Click()
Picture1.Cls
End Sub


Storing Images In Memory Using The Picture Object
You want to load a number of images into your program, SuperDuperGraphicsPro, and store them in the background, invisibly. How do you do that?
Visual Basic offers a number of ways of loading in images and storing them unobserved (all of them covered in this book, of course). You can use the image list control to store images, or the picture clip controls (picture clips are covered in this chapter). You can even load images into picture boxes and make those picture boxes invisible (by setting their Visible properties to False). And you can use Picture objects. In fact, in some ways, you can think of the Picture object as an invisible picture box that takes up far fewer system resources (although Picture objects don’t have drawing methods like Line or Circle, like picture boxes). The Picture object supports bitmaps, GIF images, JPEG images, metafiles, and icons.



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.



Wyszukiwarka

Podobne podstrony:
6 rozB 617 635
617 (2)
621 Golden Puppy
616 617
621 623
621 623
613 617
617 Dywersyfikacja
619 621

więcej podobnych podstron