Visual Basic .NET - PictureBox Control

The PictureBox control in VB.NET is used to display images on the form. It allows you to load images from files, streams, or URLs and display them in your application. Here are some common properties, methods, and events of the PictureBox control:

Properties:

  • Image: Gets or sets the image that is displayed by the control.
  • SizeMode: Gets or sets a value indicating how the image is displayed within the control.
  • BorderStyle: Gets or sets the border style of the control.
  • SizeMode: Gets or sets a value indicating how the image is displayed within the control.
  • BackgroundImage: Gets or sets the background image displayed in the control.
  • BackgroundImageLayout: Gets or sets the layout style of the background image for the control.

Methods:

  • Load: Loads the specified image from a file, stream, or URL.
  • Dispose: Releases all resources used by the control.

Events:

  • Click: Occurs when the control is clicked.
  • DoubleClick: Occurs when the control is double-clicked.
  • MouseEnter: Occurs when the mouse pointer enters the control.
  • MouseLeave: Occurs when the mouse pointer leaves the control.
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' Load the image from the file and display it in the PictureBox control
        PictureBox1.Image = Image.FromFile("C:\Users\Public\Pictures\Sample Pictures\Desert.jpg")
        ' Set the size mode to stretch image
        PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
    End Sub
End Class

In this example, we load an image from the file and display it in the PictureBox control. We also set the size mode to stretch image so that the image is stretched to fit the size of the control.

Assuming you have a PictureBox control named picImage on your form, you can display an image in it by following these steps:

Open the form's code-behind file and add the following code at the top to import the System.Drawing namespace:

Imports System.Drawing

Load the image you want to display into a Bitmap object. You can use the Bitmap.FromFile() method to load an image from a file:

Dim myImage As Bitmap = Bitmap.FromFile("C:\MyImages\myImage.jpg")

Set the Image property of the picImage control to the Bitmap object you just created:

picImage.Image = myImage

Optionally, you can resize the image to fit the PictureBox control by setting the SizeMode property to PictureBoxSizeMode.StretchImage:

picImage.SizeMode = PictureBoxSizeMode.StretchImage

This will stretch or shrink the image to fit the PictureBox control while maintaining its aspect ratio.

Here's the complete code:

Imports System.Drawing
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' Load the image from a file
        Dim myImage As Bitmap = Bitmap.FromFile("C:\MyImages\myImage.jpg")
        
        ' Display the image in the PictureBox control
        picImage.Image = myImage
        
        ' Stretch the image to fit the PictureBox control
        picImage.SizeMode = PictureBoxSizeMode.StretchImage
    End Sub
End Class

Note: Make sure to replace "C:\MyImages\myImage.jpg" with the actual path and filename of the image you want to display.