Mamun Sardar
Mamun Sardar

Reputation: 2729

How to Display an image in a picturebox from another folder?

I have an image "back.jpg" in the location: projectfolder/image/back.jpg and I'm trying to load the image on a picturebox. I'm trying the following code but It didn't work.

this.pictureBox.ImageLocation = @"\image\back.jpg";

If I have the Image in H:\back.jpg . The following code is working

this.pictureBox.ImageLocation = @"H:\back.jpg";

What's wrong with this code?this.pictureBox.ImageLocation = @"\image\back.jpg";

Upvotes: 1

Views: 7259

Answers (2)

Max Shmelev
Max Shmelev

Reputation: 3770

Your path should be: @"..\..\image\av.jpg", because you're running your application from "Folder\bin\debug" folder, and the image is 2 folders higher.

Your path @"\image\back.jpg" means that your're referring the root folder, i.e. you're targeting at "H:\image\back.jpg".

The best pattern here would be:

  1. Include the image file to your solution in VS (if you haven't done it yet)
  2. Go to the included file properties and set "Copy if newer" to "Copy if newer"
  3. Use this code: this.pictureBox.ImageLocation = "back.jpg";

Upvotes: 3

Amiram Korach
Amiram Korach

Reputation: 13296

Set the image file to be copied to the output directory always and refer to it only with the file name.

Upvotes: 0

Related Questions