Search In Site

01 July, 2013

working with Images in Visual Basic

Visual Basic has quite an array of techniques for dealing with images. In this chapter, we’ll work with bitmapped images in our programs, creating some powerful effects. We’ll see how to load images in, display them in a variety of ways, including flipping them and stretching them, creating image effects, and saving them back to disk. Images can be an asset to your program, enhancing the visual interface a great deal. We won’t work on creating images here instead, we’ll work on reading them in, working on them, and displaying them from image files on disk.
There are a number of different image formats that you use today: bitmap (.bmp), GIF, JPEG, WMF (Windows metafile format), enhanced WMF, icon (.ico), compressed bitmap (.rle), and more. Visual Basic can handle all these formats.
However, you’ll notice some anachronisms that have crept in over the years that indicate Visual Basic’s historical development 
for example, the picture clip control, which we’ll see in this chapter, can only handle bitmaps with a maximum of 16 colors. This control is still a useful one, but it has largely been superseded by the more powerful image list control.

Picture Boxes Vs. Image Controls
The main controls that programmers use to display images are image controls and picture boxes. That’s not to say there aren’t other ways to display, of course: you can load images into many controls, like buttons, and even display them in forms, as we’ll see in this chapter. However, when  programmers think of displaying and working with images, they often think of picture boxes and image controls. It’s worth noting the difference between these controls. The image control really has one main purpose: to display images. If that’s your goal, the image control is a good choice. On the other hand, picture boxes offer you a great deal more, if you need it. You can even think of picture boxes as mini-paint programs, because they include methods to let you draw text (on top of the current image in the picture box, which is good if you want to label elements in that image), draw circles, lines, boxes, and so on. 
Note, however, that the added power of picture boxes comes with an added cost in terms of heavier use of system resources. If you don’t need a picture box’s added functionality, use an image control.

Image Effects: Working With Images Bit By Bit
In this chapter, we’ll have some fun seeing how to work with images bit by bit. There are two main ways of doing that in Visual Basic: sticking with the Visual Basic methods, and using Windows methods directly.We’ll stick with the Visual Basic methods, which, although slower, are vastly easier to use and get the job done well. However, you should know that we’ll take a look at the Windows way of doing things later in the book, in the chapter on  connecting to Windows directly. (And you may have noticed our bitmapped menu item example in the chapter on menus works directly with Windows to create a bitmap object that it loads into a menu.) We’ll see quite a few image effects in this chapter: embossing images, engraving images, grayscale Images, image lightening, blurring images, making an image seem to sweep from upper left to lower right, and more. All these effects are powerful techniques that you might not expect from Visual Basic.  That’s it for the overview of images for the moment it’s time to turn to the Immediate Solutions.

Adding Images To Controls
The Aesthetic Design Department is calling again. Can’t you add some images to the controls in your program? That would make it look so much nicer.
These days, you can add images to many Visual Basic controls. For example, you can now display images in checkboxes, command buttons, and option buttons if you first set their Style property to Graphical (Style = 1), then place the name of the image file you want to use in the control’s Picture property.
At runtime, you can load a picture into the control’s Picture property using the LoadPicture function:
Private Sub Command1_Click()
Command1.Picture = LoadPicture("c:\image.bmp")
End Sub

Besides buttons, you can also display images in the Visual Basic image combo box The Windows common controls can also display images,including such controls as tree views, list views, and tab strips. There, you load the images you want into an image list control, and then connect that image list to the control using the control’s ImageList property. For more information,

Adding Images To Forms
The Aesthetic Design Department is on the phone again. The form in your program looks pretty drab. How about spicing it up with an image of the company founder? Hmm, you wonder, how would you do that? You can load an image into a form using the form’s Picture property, both at design time or at runtime. As an example, we’ve placed an image in the form. Note that the controls on that form are layered on top of the form’s image. At runtime, you can use the LoadPicture function to read in an image and display it in a form like this:
Private Sub Command1_Click()
Form1.Picture = LoadPicture("c:\image.bmp")
End Sub


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

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. 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.

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

0 comments:

Post a Comment

Dear Visitors All The Tricks And Hacks Posted Here Are Only For Knowledge Purpose.Don't Use These for Illegal Operations.

 
Twitter Bird Gadget