PictureBox Control in VB.Net

The PictureBox control in Windows Forms serves as a powerful tool for displaying various image formats such as bitmaps, GIFs, icons, or JPEGs. You have the flexibility to set the Image property of the PictureBox to the desired image, either during design time or at runtime.

Image property

The Image property allows you to specify the image that you want to be displayed within the PictureBox. This capability empowers you to dynamically change the image being shown based on different conditions or user interactions. This feature becomes particularly useful when utilizing a single form to display different pieces of information, as you can programmatically alter the image shown in the PictureBox to reflect the relevant content.

PictureBox1.Image = Image.FromFile("C:\testImage.jpg")

Whether it's displaying static images or dynamically changing them, the PictureBox control offers a versatile solution for image representation within a Windows Forms application, providing an effective means of conveying information visually.

The SizeMode property, which is set to values in the PictureBoxSizeMode enumeration, controls the clipping and positioning of the image in the display area.

PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage

There are five different PictureBoxSizeMode is available to PictureBox control.

  1. AutoSize - Sizes the picture box to the image.
  2. CenterImage - Centers the image in the picture box.
  3. Normal - Places the upper-left corner of the image at upper left in the picture box.
  4. StretchImage - Allows you to stretch the image in code.

You can change the size of the display area at run time with the ClientSize property.

pictureBox1.ClientSize = New Size(xSize, ySize)

The following VB.Net program shows how to load a picture from a file and display it in streach mode.

Full Source VB.NET
Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load PictureBox1.Image = Image.FromFile("d:\testImage.jpg") PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage End Sub End Class


By programmatically changing the image displayed in the PictureBox control, you can provide a dynamic and interactive user interface experience. This allows for the seamless transition between different images, enabling the PictureBox to adapt and display the appropriate visual content based on the application's logic or user input.