Lesson 7: Working with PictureBox in VB2019
Master image display and manipulation to create visually rich applications
Key Takeaway
The PictureBox control is essential for displaying images in Visual Basic applications. Mastering its properties and methods allows you to create dynamic image viewers and enhance your application's visual appeal.
In Visual Basic 2019, the PictureBox control is used to display images in various formats. This lesson explores how to effectively implement and manipulate images using the PictureBox control at both design time and runtime.
7.1 Loading Images in PictureBox
The PictureBox control provides versatile options for displaying images. You can load images at design time for static content or load them dynamically at runtime for interactive applications.
Design Time Loading
Add images directly in the Visual Studio designer
Runtime Loading
Programmatically load images during execution
File Dialog Integration
Let users browse and select images
Size Modes
Control how images fit in the PictureBox
7.1.1 Loading an Image at Design Time
To load an image at design time, follow these steps:
1 Insert PictureBox
Add a PictureBox control to your form. Set its border to FixedSingle and background color to white.
2 Configure Properties
Set the SizeMode property to StretchImage to ensure images fit properly within the control.
3 Select Image
In the Properties window, click the Image property and browse for your image file.
4 Apply Image
Select your image and click Open to display it in the PictureBox.


Important Notes
SizeMode Property: Determines how the image is displayed (Normal, StretchImage, AutoSize, CenterImage, Zoom).
Supported Formats: PictureBox supports JPG, PNG, BMP, GIF, and other common image formats.
Resource Management: Images added at design time are embedded in your application resources.
7.1.2 Loading an Image at Runtime
You can load images dynamically using the Image.FromFile
method:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load ' Load an image from a file path MyPicBox.Image = Image.FromFile("C:\Users\Documents\vb2019\vb2019images\fish.jpg") End Sub
Output:
Important Considerations
File Paths: Use absolute paths for development, but consider relative paths for deployment.
Error Handling: Always implement error handling for missing files or invalid paths.
Resource Management: Dispose of previous images to prevent memory leaks.
7.2 Using OpenFileDialog with PictureBox
To let users browse and select images, use the OpenFileDialog control:
1 Add OpenFileDialog
Drag the OpenFileDialog control from the toolbox to your form.
2 Configure Filter
Set the Filter property to display only image files:
JPEG Files|*.JPG|GIF Files|*.GIF|Windows Bitmaps|*.BMP|PNG Files|*.PNG
3 Add Browse Button
Create a button that triggers the file dialog when clicked.
4 Load Selected Image
Use the selected file path to load the image into the PictureBox.


Code to implement image browsing:
Private Sub BtnBrowse_Click(sender As Object, e As EventArgs) Handles BtnBrowse.Click ' Configure and show the OpenFileDialog If OFDSelectImage.ShowDialog() = DialogResult.OK Then ' Load the selected image Try ' Dispose of previous image to prevent memory leaks If MyPicBox.Image IsNot Nothing Then MyPicBox.Image.Dispose() ' Load the new image MyPicBox.Image = Image.FromFile(OFDSelectImage.FileName) Catch ex As Exception ' Handle errors (e.g., invalid image format) MessageBox.Show("Error loading image: " & ex.Message) End Try End If End Sub
Output:
> Image loaded from selected path
PictureBox Demo
Try loading different images to see how they appear in the PictureBox control.
No image loaded
Lesson Summary
In this lesson, you've learned how to effectively work with the PictureBox control:
Design Time Loading
Add images directly through the Visual Studio properties window
Runtime Loading
Programmatically load images using the Image.FromFile method
File Dialog Integration
Implement the OpenFileDialog control for user image selection
Practical Applications
Create image viewers, photo galleries, and visually rich applications
You've learned how to work with the PictureBox control to display and manage images in your applications. In the next lesson, we'll explore Visual Basic data types and how to use them effectively.
Next Lesson
Ready to learn about data types? Continue to Lesson 8: Data Types.
Related Resources

Visual Basic 2019 Made Easy
Unlock the power of Visual Basic 2019 with this comprehensive, easy-to-follow handbook written by Dr. Liew, renowned educator and founder of the popular programming tutorial website VBtutor.net. Whether you're new to programming or brushing up your skills, this book is your perfect companion to learn Visual Basic 2019 from the ground up.
What You'll Learn:
- Understand Core Programming Concepts: Grasp the foundational principles of Visual Basic 2019, including variables, data types, conditional logic, loops, and event-driven programming.
- Develop Real Windows Desktop Applications: Build fully functional and interactive Windows apps using Visual Studio 2019—guided through step-by-step tutorials.
- Apply Dozens of Ready-to-Use Examples: Explore a rich collection of practical sample programs, from basic calculators to image viewers and database applications.
- Adapt and Reuse Code for Your Own Projects: Customize professionally written code snippets to speed up your development process and bring your ideas to life.
- Package and Deploy Like a Pro: Learn how to compile, test, and distribute your Visual Basic applications seamlessly with built-in deployment tools.

Visual Basic Programming With Code Examples
Visual Basic Programming with Code Examples offers a unique dual-format approach, showcasing sample codes in both Visual Basic 6 (VB6) and VB.NET. This side-by-side presentation helps you understand the evolution of Visual Basic and empowers you to work confidently across both environments.
What You'll Learn:
- Core Concepts Made Easy: Explore data types, control structures, file handling, procedures, user interface design, and more.
- Hands-On Application Building: Design real-world applications, including financial calculators, educational tools, games, multimedia apps, and database systems.
- 48 Practical Code Examples: Study and customize fully explained programs that illustrate key programming techniques.
- Dual-Code Format: Learn to translate and adapt code between VB6 and VB.NET seamlessly.