How to Use pictureBox in VB net?
How to Use PictureBox in VB Net?
Are you new to VB Net and looking for a detailed guide on how to use the PictureBox control? If so, you’re in the right place. The PictureBox control is an essential tool for displaying images in VB Net, and it’s used extensively in graphical applications.
In this article, we’ll cover everything you need to know about using the PictureBox control in VB Net. From its basic functions to more advanced techniques, we’ll guide you through the process step-by-step. So, let’s get started.
Introduction
The PictureBox control is a graphical control that allows you to display images in VB Net. It’s a versatile tool that can be used in a variety of applications, such as image viewers, multimedia applications, and games.
In this article, we’ll cover the basics of using the PictureBox control, including how to add it to your form, how to load images into it, and how to customize its properties. We’ll also cover more advanced techniques, such as using PictureBox events and methods.
What is PictureBox?
PictureBox is a graphical control that displays images in VB Net. It’s a part of the System.Windows.Forms namespace, which provides a collection of classes for building Windows-based applications.
The PictureBox control is highly customizable, allowing you to set its properties, events, and methods. It supports a variety of image formats, including BMP, GIF, JPEG, PNG, and TIFF.
Setting Up Your Environment
Before we get started, you’ll need to set up your environment for VB Net programming. You’ll need to download and install Visual Studio, which is an Integrated Development Environment (IDE) that provides a comprehensive set of tools for building Windows-based applications.
Once you’ve installed Visual Studio, you’ll need to create a new VB Net project. Open Visual Studio and select File > New > Project. In the New Project dialog box, select Windows Forms App (.NET Framework), and give your project a name. Click Create to create your project.
Adding a PictureBox Control to Your Form
To add a PictureBox control to your form, you’ll need to open the Toolbox window in Visual Studio. The Toolbox window contains a collection of controls that you can add to your form.
To open the Toolbox window, select View > Toolbox from the menu bar. In the Toolbox window, locate the PictureBox control, and drag it onto your form.
Loading Images into PictureBox
To load an image into PictureBox, you’ll need to create an Image object and set its value to the Image property of the PictureBox control. Here’s an example:
Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim img As Image = Image.FromFile("C:\Paul O’Grady1.jpg") PictureBox1.Image = img End Sub End Class
Run the app and you will see the output as given below:

In this example, we create an Image object named img, which is loaded from a file located at “C:\Paul O’Grady1.jpg”. We then set the Image property of the PictureBox control to img, which displays the image in the control.
Setting the PictureBox Properties
The PictureBox control supports a variety of properties that you can use to customize its appearance and behavior. Here are some of the most commonly used properties:
- SizeMode: Specifies how the image is displayed in the control. The SizeMode property accepts a value from the PictureBoxSizeMode enumeration, which includes values such as StretchImage
- Location: Specifies the position of the control on the form. The Location property accepts a Point object, which represents the x and y coordinates of the control’s upper-left corner.
- BackColor: Specifies the background color of the control.
- BorderStyle: Specifies the border style of the control.
- Enabled: Specifies whether the control can be used by the user.
- Visible: Specifies whether the control is visible on the form.
You can set these properties using the Properties window in Visual Studio. Select the PictureBox control on your form, and then locate the Properties window. You can set the properties by clicking on the corresponding values in the Properties window.
Using PictureBox Events
The PictureBox control supports a variety of events that you can use to respond to user actions or changes in the control’s state. Here are some of the most commonly used events:
- Click: Occurs when the user clicks the control.
- DoubleClick: Occurs when the user double-clicks the control.
- MouseEnter: Occurs when the mouse pointer enters the control’s boundaries.
- MouseLeave: Occurs when the mouse pointer leaves the control’s boundaries.
- SizeChanged: Occurs when the control’s size changes.
To use an event, you’ll need to write an event handler. An event handler is a piece of code that executes when the event occurs. Here’s an example:
Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim img As Image = Image.FromFile("C:\Paul O’Grady1.jpg") PictureBox1.Image = img End Sub Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click MessageBox.Show("You clicked me.") End Sub End Class
Run the app and click the image to see the following output:

In this example, we create an event handler for the Click event of the PictureBox control. The event handler is called when the user clicks the control, and it executes the code inside the handler.
Using PictureBox Methods
The PictureBox control also supports a variety of methods that you can use to perform actions on the control. Here are some of the most commonly used methods:
- Load: Loads an image into the control from a file or URL.
- Dispose: Releases the resources used by the control.
- Refresh: Refreshes the control’s display.
- Update: Updates the control’s display.
You can use these methods by calling them in your code. Here’s an example:
Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 'Dim img As Image = Image.FromFile("C:\Paul O’Grady1.jpg") ' PictureBox1.Image = img End Sub Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click ' MessageBox.Show("You clicked me.") End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 'First of all drag and drop a button from the toolbox. change the button text in the properties windows to Load Image(optional) 'then double click the button and code it as below. PictureBox1.Load("C:\Paul O’Grady1.jpg") End Sub End Class
Run the app and click the “Load Image” button to see the following output:

In this example, we use the Load method to load an image into the PictureBox control from a file located at “C:\Paul O’Grady1.jpg”.
Advanced Techniques
Once you’ve mastered the basics of using the PictureBox control, you can move on to more advanced techniques. Here are some examples:
- Animating images: You can use the Timer control to animate images in the PictureBox control.
- Custom painting: You can use the Paint event to draw custom graphics on the PictureBox control.
- Zooming and panning: You can use the MouseDown, MouseMove, and MouseUp events to implement zooming and panning functionality in the PictureBox control.
For Example to animate image in a picturebox, you should follow the following steps:
1. First, add a PictureBox control to your form by dragging and dropping it from the toolbox.
2. Next, add the images you want to use for your animation to your project’s resources. To do this, right-click on your project in the Solution Explorer and select “Properties”. Then, select the “Resources” tab and click the “Add Resource” dropdown. Choose “Add Existing File” and select your images.

3. Drage and drop a timmer object to control the animation. Set its interval property to the desired animation speed, in milliseconds. In this example, we’ll use a timer interval of 100 milliseconds.
Private currentFrame As Integer = 0 Private frames As Integer = 4 'number of frames in the animation
4. In the timer’s tick event, update the PictureBox control’s Image property to display the next frame of the animation. Use a switch statement to handle each frame of the animation, and reset the current frame counter when you reach the end of the animation.
Private Sub timer1_Tick(sender As Object, e As EventArgs) Handles timer1.Tick Select Case currentFrame Case 0 PictureBox1.Image = My.Resources.frame1 Case 1 PictureBox1.Image = My.Resources.frame2 Case 2 PictureBox1.Image = My.Resources.frame3 Case 3 PictureBox1.Image = My.Resources.frame4 End Select currentFrame += 1 If currentFrame = frames Then currentFrame = 0 End If End Sub
5. Finally, start and stop the timer as needed to begin and end the animation. For example, you could start the timer in the form’s Load event and stop it in the form’s FormClosing event.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load timer1.Interval = 100 'set the timer interval to 100ms timer1.Start() 'start the timer End Sub Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing timer1.Stop() 'stop the timer when the form is closing End Sub
And that’s it! With this code, you should be able to animate images in a PictureBox control in your VB.Net application. The overall code structure in your code behind should look like:
Public Class Form1 Private currentFrame As Integer = 0 Private frames As Integer = 4 'number of frames in the animation Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing Timer1.Stop() 'stop the timer when the form is closing End Sub Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Timer1.Interval = 100 'set the timer interval to 100ms Timer1.Start() 'start the timer End Sub Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick Select Case currentFrame Case 0 PictureBox1.Image = My.Resources.frame1 Case 1 PictureBox1.Image = My.Resources.frame2 Case 2 PictureBox1.Image = My.Resources.frame3 Case 3 PictureBox1.Image = My.Resources.frame4 End Select currentFrame += 1 If currentFrame = frames Then currentFrame = 0 End If End Sub End Class
Now run the app and check the output yourself. I have checked it on my enviroment and it is 100% okay.
Conclusion
The PictureBox control is an essential tool for displaying images in VB Net. In this article, we’ve covered the basics of using the control, including how to add it to your form, load images into it, and customize its properties. We’ve also covered more advanced techniques, such as using events and methods.
By mastering the PictureBox control, you can create powerful graphical applications that impress your users. So, start practicing and experiment with the techniques we’ve covered in this article.
Overall, the PictureBox control is a versatile and powerful tool that can be used to display images in VB Net applications. By learning how to use its properties, events, and methods, you can create dynamic and visually appealing applications. With the techniques covered in this article, you should be well on your way to mastering the PictureBox control and creating impressive applications.
FAQs
- Can I display animated GIFs in the PictureBox control?
Yes, you can use the ImageAnimator class to display animated GIFs in the PictureBox control.
- Can I resize the PictureBox control at runtime?
Yes, you can use the Size property to resize the control dynamically.
- Can I change the border color of the PictureBox control?
No, the BorderColor property is not supported by the PictureBox control. However, you can use the BorderStyle property to change the style of the border.
- Can I use the PictureBox control to display video?
No, the PictureBox control is designed to display images, not video. To display video, you’ll need to use a different control or library.
- How do I clear the image from the PictureBox control?
You can use the Image property to set the control’s image to null or use the Dispose method to release the control’s resources.