PictureBox and Image Controls

 Lesson 25 

Today I'll show you the usage of PictureBox and Image controls. They are used to display images, you guessed it? But there are some differences between the two controls. There are certain cases when the Image control is suitable, and in some other, the PictureBox control will make your task easier, though both of them share the same job. So lets dig into some details!



Where to use images?

The first plain fact to know is that the Image and PictureBox controls are used to display pictures or icons on the form. You develop a software and you don't use any picture, that does not make sense. You generally use pictures or icons for the company logo and to make your software more graphical that'll surely make your application user-friendly. You can make use of the images on the splash screen and in many other cases.
 


Supported Picture Formats

The supported picture formats for these control include BMP, DIB (bitmap), ICO (icon), CUR (cursor), WMF (metafile), EMF (enhanced metafile), GIF and JPEG. You can't use a PNG format.
 

Firstly I'll talk about the PictureBox control and then the Image control. Lastly the comparison and differentiation between them will be discussed along with the explanation of when to use one or another.



The PictureBox control

The following points describe a PictureBox control:
  • The pictureBox control comes with a light border around it.
  • Works as a container: You can place any other control on it, when you move the PictureBox control, all the controls on it moves along with. That means, it works as a container of other controls. 
  • Similarity with the Form: This control is so similar to a form as both of them support all the graphical properties, graphical methods and conversion methods. 
  • Supports all the graphical properties: It supports all the graphical properties such as AutoRedraw, ClipControls, HasDC, FontTransparent, CurrentX, CurrentY, and the Drawxxxx, Fillxxxx, and Scalexxxx properties.
  • Supports all the graphical methods: It supports all the graphical methods such as Cls, PSet, Point, Line, and Circle. 
  • Supports the conversion methods: It supports the conversion methods such as ScaleX, ScaleY, TextWidth, and TextHeight.
  • Loading images: Using the Picture property, you can load a picture in design time and run-time. The LoadPicture function is used to load the picture from your computer. This function is used with both the PictureBox and Image controls.

Example:

Picture1.Picture = LoadPicture("D:\\PictureName.jpg")

Example:

Image1.Picture = LoadPicture("D:\\PictureName.jpg")
  • Clearing the current image: You can clear the current image of the PictureBox or Image control in design time by clearing the value of Picture property. This can also be done in run-time in the following way.
Picture1.Picture=LoadPicture("")

or

set Picture1.picture=Nothing


 Download this sample: Image Show

 

 

The Image Control

The image control has almost the same features of PictureBox.
  • Loading Images: You load an image in the same way.
Example:
Image1.Picture = LoadPicture("D:\\PictureName.jpg")
  • The Stretch property: If you set the Stretch property to true, it stretches the picture to fit in the control. 


How the Image control is different from PictureBox?

  1. Image controls are less complex than PictureBox controls.
  2. The Image control does not have border around it like a PictureBox control.
  3. The image control does not work as a container.
  4. The Image controls do not have graphical properties, graphical methods or conversion methods such as ScaleX, ScaleY, TextWidth, and TextHeight.
  5. The PictureBox control does not have the Stretch property. 
  6. The Image control comparatively takes less memory, so it loads faster.


Copying an image from one control to another

You can assign the Picture property of one control to another.

Example:
Picture2.Picture = Picture1.Picture

Or,

Image2.Picture = Image1.Picture


As a conclusion I want to say that many Visual Basic programmers prefer the Image control over PictureBox as the Image control consumes less memory resource resulting a faster loading of it. Besides, it supports all the events a pictureBox can have, and you can even have a border around it from the BorderStyle property.

The only big limitation is that it cannot work as a container. For this reason, the PictureBox control can be sometimes preferred much. But above all, you should always choose one which is appropriate for your application.


Related topic:





Like my facebook page: www.facebook.com/vbtutes for free updates!