VB.Net - PictureBox Control

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.
The following are some of the commonly used properties of the PictureBox control −
AllowDrop
Specifies whether the picture box accepts data that a user drags on it. 
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.
Image
Gets or sets the image that is displayed in the control.
ImageLocation
Gets or sets the path or the URL for the image displayed in the control.
InitialImage
Gets or sets the image displayed in the control when the main image is loaded.
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
Normal − the upper left corner of the image is placed at upper left part of the picture box
StrechImage − allows stretching of the image
StrechImage − allows stretching of the image
AutoSize − allows resizing the picture box to the size of the image
AutoSize − allows resizing the picture box to the size of the image
CenterImage − allows centering the image in the picture box
CenterImage − allows centering the image in the picture box
Zoom − allows increasing or decreasing the image size to maintain the size ratio.
Zoom − allows increasing or decreasing the image size to maintain the size ratio.
TabIndex
Gets or sets the tab index value.
TabStop
Specifies whether the user will be able to focus on the picture box by using the TAB key.
Text
Gets or sets the text for the picture box.
WaitOnLoad
Specifies whether or not an image is loaded synchronously.
The following are some of the commonly used methods of the PictureBox control −
CancelAsync
Cancels an asynchronous image load.
Load
Displays an image in the picture box
LoadAsync
Loads image asynchronously.
ToString
Returns the string that represents the current picture box.
The following are some of the commonly used events of the PictureBox control −
CausesValidationChanged
Overrides the Control.CausesValidationChanged property.
Click
Occurs when the control is clicked.
Enter
Overrides the Control.Enter property.
FontChanged
Occurs when the value of the Font property changes.
ForeColorChanged
Occurs when the value of the ForeColor property changes.
KeyDown
Occurs when a key is pressed when the control has focus.
KeyPress
Occurs when a key is pressed when the control has focus.
KeyUp
Occurs when a key is released when the control has focus.
Leave
Occurs when input focus leaves the PictureBox.
LoadCompleted
Occurs when the asynchronous image-load operation is completed, been canceled, or raised an exception.
LoadProgressChanged
Occurs when the progress of an asynchronous image-loading operation has changed.
Resize
Occurs when the control is resized.
RightToLeftChanged
Occurs when the value of the RightToLeft property changes.
SizeChanged
Occurs when the Size property value changes.
SizeModeChanged
Occurs when SizeMode changes.
TabIndexChanged
Occurs when the value of the TabIndex property changes.
TabStopChanged
Occurs when the value of the TabStop property changes.
TextChanged
Occurs when the value of the Text property changes.
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 −
When the application is executed, it displays −
Clicking on the button results in −

    63 Lectures 
     4 hours 

 Frahaan Hussain

    103 Lectures 
     12 hours 

 Arnold Higuit

    60 Lectures 
     9.5 hours 

 Arnold Higuit

    97 Lectures 
     9 hours 

 Arnold Higuit
 Print
 Add Notes
 Bookmark this page