• We just launched and are currently in beta. Join us as we build and grow the community.

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

jengger3000

Mecha Blueprints Collector
J Rep
0
0
0
Rep
0
J Vouches
0
0
0
Vouches
0
Posts
173
Likes
84
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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#.
creating_application_c_0.png

Step 2

Add a PictureBox ,OpenFileDialog and Button into the form.
2018-11-09.png

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. //filter the file you want to get
  2. openFileDialog1.

    Filter

    =

    "img (*.jpg)|*.jpg|All files (*.*)|*.*"

    ;
  3. //set an intial directory
  4. openFileDialog1.

    InitialDirectory

    =

    @"C:\"

    ;
  5. //Put the title in the dialog box
  6. openFileDialog1.

    Title

    =

    "Please select an image."

    ;
  7. //validating result
  8. if

    (

    openFileDialog1.

    ShowDialog

    (

    )

    ==

    DialogResult.

    OK

    )
  9. {
  10. //set the image to be stretch
  11. pictureBox1.

    SizeMode

    =

    PictureBoxSizeMode.

    StretchImage

    ;
  12. //load the image in the picture box
  13. pictureBox1.

    Image

    =

    Image.

    FromFile

    (

    openFileDialog1.

    FileName

    )

    ;
  14. }

For any questions about this article. You can contact me @
Email – [email protected]
Mobile No. – 09305235027 – TNT
Or feel free to comment below.

 

442,401

317,942

317,951

Top