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 −

Sr.No. Property & Description
1

AllowDrop

Specifies whether the picture box accepts data that a user drags on it.

2

ErrorImage

Gets or specifies an image to be displayed when an error occurs during the image-loading process or if the image load is cancelled.

3

Image

Gets or sets the image that is displayed in the control.

4

ImageLocation

Gets or sets the path or the URL for the image displayed in the control.

5

InitialImage

Gets or sets the image displayed in the control when the main image is loaded.

6

SizeMode

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

7

TabIndex

Gets or sets the tab index value.

8

TabStop

Specifies whether the user will be able to focus on the picture box by using the TAB key.

9

Text

Gets or sets the text for the picture box.

10

WaitOnLoad

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 −

Sr.No. Method Name & Description
1

CancelAsync

Cancels an asynchronous image load.

2

Load

Displays an image in the picture box

3

LoadAsync

Loads image asynchronously.

4

ToString

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 −

Sr.No. Event & Description
1

CausesValidationChanged

Overrides the Control.CausesValidationChanged property.

2

Click

Occurs when the control is clicked.

3

Enter

Overrides the Control.Enter property.

4

FontChanged

Occurs when the value of the Font property changes.

5

ForeColorChanged

Occurs when the value of the ForeColor property changes.

6

KeyDown

Occurs when a key is pressed when the control has focus.

7

KeyPress

Occurs when a key is pressed when the control has focus.

8

KeyUp

Occurs when a key is released when the control has focus.

9

Leave

Occurs when input focus leaves the PictureBox.

10

LoadCompleted

Occurs when the asynchronous image-load operation is completed, been canceled, or raised an exception.

11

LoadProgressChanged

Occurs when the progress of an asynchronous image-loading operation has changed.

12

Resize

Occurs when the control is resized.

13

RightToLeftChanged

Occurs when the value of the RightToLeft property changes.

14

SizeChanged

Occurs when the Size property value changes.

15

SizeModeChanged

Occurs when SizeMode changes.

16

TabIndexChanged

Occurs when the value of the TabIndex property changes.

17

TabStopChanged

Occurs when the value of the TabStop property changes.

18

TextChanged

Occurs 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
vb.net_basic_controls.htm
Advertisements