How to Load an Image from the Local Directory into the PictureBox in C#

In this tutorial, I will teach you how to load an image from the local directory into the picturebox in c#. In here , I used the openfiledialog to access the local directory. In this way, I can get an image and put it into the picturebox.

Creating Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application in c#. loadimage1

Step 2

Add a PictureBox ,OpenFileDialog and Button into the form. loadimage2

Step 3

Double click the button to fire the click event handler of it and do the following code to get an image from the local directory and load it into the picturebox.
  1.  
  2.             //filter the file you want to get
  3.             openFileDialog1.Filter = "img (*.jpg)|*.jpg|All files (*.*)|*.*";
  4.             //set an intial directory
  5.             openFileDialog1.InitialDirectory = @"C:\";
  6.             //Put the title in the dialog box
  7.             openFileDialog1.Title = "Please select an image.";
  8.             //validating result
  9.             if (openFileDialog1.ShowDialog() == DialogResult.OK)
  10.             {
  11.                 //set the image to be stretch
  12.                 pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
  13.                 //load the image in the picture box
  14.                 pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
  15.             }
For any questions about this article. You can contact me @ Email – [email protected] Mobile No. – 09305235027 – TNT Or feel free to comment below.

Add new comment