VB.Net - PictureBox Control

Advertisements


The PictureBox control is used for displaying images on the form. The Image property of the control allows you to set an image both at design time or at run time.

Let's create a picture box by dragging a PictureBox control from the Toolbox and dropping it on the form.

VB.Net PictureBox Control

Properties of the PictureBox Control

The following are some of the commonly used properties of the PictureBox control:

S.NPropertyDescription
1AllowDropSpecifies whether the picture box accepts data that a user drags on it.
2ErrorImageGets or specifies an image to be displayed when an error occurs during the image-loading process or if the image load is cancelled.
3ImageGets or sets the image that is displayed in the control.
4ImageLocationGets or sets the path or the URL for the image displayed in the control.
5InitialImageGets or sets the image displayed in the control when the main image is loaded
6SizeModeDetermines the size of the image to be displayed in the control. This property takes its value from the PictureBoxSizeMode enumeration, which has values:
  • Normal - the upper left corner of the image is placed at upper left part of the picture box
  • StrechImage - allows stretching of the image
  • AutoSize - allows resizing the picture box to the size of the image
  • CenterImage - allows centering the image in the picture box
  • Zoom - allows increasing or decreasing the image size to maintain the size ratio.
7TabIndexGets or sets the tab index value.
8TabStopSpecifies whether the user will be able to focus on the picture box by using the TAB key.
9TextGets or sets the text for the picture box.
10WaitOnLoad Specifies whether or not an image is loaded synchronously.

Methods of the PictureBox Control

The following are some of the commonly used methods of the PictureBox control:

S.NMethod Name & Description
1CancelAsync
Cancels an asynchronous image load.
2Load
Displays an image in the picture box
3LoadAsync
Loads image asynchronously.
4ToString
Returns the string that represents the current picture box.

Events of the PictureBox Control

The following are some of the commonly used events of the PictureBox control:

S.NEventDescription
1CausesValidationChangedOverrides the Control.CausesValidationChanged property.
2ClickOccurs when the control is clicked.
3EnterOverrides the Control.Enter property.
4FontChangedOccurs when the value of the Font property changes.
5ForeColorChangedOccurs when the value of the ForeColor property changes.
6KeyDownOccurs when a key is pressed when the control has focus.
7KeyPressOccurs when a key is pressed when the control has focus.
8KeyUpOccurs when a key is released when the control has focus.
9LeaveOccurs when input focus leaves the PictureBox.
10LoadCompletedOccurs when the asynchronous image-load operation is completed, been canceled, or raised an exception.
11LoadProgressChangedOccurs when the progress of an asynchronous image-loading operation has changed.
12ResizeOccurs when the control is resized.
13RightToLeftChangedOccurs when the value of the RightToLeft property changes.
14SizeChangedOccurs when the Size property value changes.
15SizeModeChangedOccurs when SizeMode changes.
16TabIndexChangedOccurs when the value of the TabIndex property changes.
17TabStopChangedOccurs when the value of the TabStop property changes.
18TextChangedOccurs when the value of the Text property changes.

Example

In this example, let us put a picture box and a button control on the form. We set the image property of the picture box to logo.png, as we used before. The Click event of the button named Button1 is coded to stretch the image to a specified size:

Public Class Form1
   Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
      ' Set the caption bar text of the form.  
      Me.Text = "tutorialspoint.com"
   End Sub
   
   Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      PictureBox1.ClientSize = New Size(300, 300)
      PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
   End Sub
End Class

Design View:

Picture Box Example Design View

When the application is executed, it displays:

Picture Box Example

Clicking on the button results in:

Picture Box Result Form

Advertisements
Advertisements