In this article I will explain with an example, how to display Image from Folder (Directory) in PictureBox control in Windows Application using C# and VB.Net.
The Image file will be selected from Folder (Directory) using the OpenFileDialog control and then the chosen Image file will be displayed in PictureBox control using the FromFile function of the Image class.
Form Design
The below Form consists of a Button, a Label and a PictureBox control.
Namespaces
You will need to import the following namespaces.
C#
using System.IO;
using System.Drawing;
VB.Net
Imports System.IO
Imports System.Drawing
Displaying Images from Folder (Directory) in PictureBox
When the Choose File Button is clicked, the OpenFileDialog is opened and the Image file is selected.
The selected Image file is displayed in PictureBox control using the FromFile function of the Image class.
C#
private void btnChoose_Click(object sender, EventArgs e)
{
using (OpenFileDialog openFileDialog1 = new OpenFileDialog())
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
lblFileName.Text = Path.GetFileName(openFileDialog1.FileName);
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
}
}
}
VB.Net
Private Sub btnChoose_Click(sender As System.Object, e As System.EventArgs) Handles btnChoose.Click
Using openFileDialog1 As OpenFileDialog = New OpenFileDialog()
If openFileDialog1.ShowDialog() = DialogResult.OK Then
lblFileName.Text = Path.GetFileName(openFileDialog1.FileName)
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName)
End If
End Using
End Sub
Screenshot
Downloads