2

I want PictureBox to load images from application folder. In the code below it loads picture from exact place. I want it to load images from application's folder so that if I copy it to other computers it could load images.

How can I do it?

Loads from exact place:

PictureBox1.Image = Image.FromFile("D:\68.jpg");

I want it to be like this:

PictureBox1.Image = Image.FromFile("ApplicationFolder\68.jpg");
Tim Williams
  • 154,628
  • 8
  • 97
  • 125
Murad Talibov
  • 31
  • 1
  • 1
  • 4
  • You want the AppDomain's current base directory: http://stackoverflow.com/questions/295687/get-path-to-execution-directory-of-windows-forms-application – codechurn Mar 13 '13 at 16:17

2 Answers2

4

Using the info in the comment above, you could do:

PictureBox1.Image = Image.FromFile(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "68.jpg"))

To do use a subdirectory of the assembly base directory:

PictureBox1.Image = Image.FromFile(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SomeFolderInBaseDirectory", "68.jpg"))
codechurn
  • 3,870
  • 4
  • 45
  • 65
  • Thanks.It works.Also I wanted to ask if I can call images from folder inside the BaseDirectory so that they don't mix with .exe files? – Murad Talibov Mar 13 '13 at 18:52
  • @MuradTalibov Please accept as answer if it solved your issue (which according to your comment it did). – codechurn May 30 '13 at 02:33
0

Use to keep images in application resources:

Add PictureBox control on Form. Select the control on the form and use properties. Find Image under the Appearance section in Properties tab and click on [...]. Select Resource dialog imports all images you're going to use in your application. Then remove the PictureBox from the Form. The application keeps all images in resources

Vadim
  • 1