29

I understand that this question has been asked (and answered) before. However, none of the solutions are working for me.

Below is a screen capture of all the relevant pieces of the puzzle:

Screen capture http://dinosaur-island.com/PlantPictureBoxScreenCap.jpg

As you can see there are numerous bitmaps of plants loaded as resources into the Images folder. There is a form with a picturebox named "PlantPicture". There is string, which I know has a good path (because I've checked it in the debugger):

            PicPath = PicPath+".bmp";

Screen capture http://dinosaur-island.com/PlantDebugger.jpg

I've tried numerous ways of loading, casting, etc., etc.

5 Answers 5

54

The path should be something like: "Images\a.bmp". (Note the lack of a leading slash, and the slashes being back slashes.)

And then:

pictureBox1.Image = Image.FromFile(@"Images\a.bmp");

I just tried it to make sure, and it works. This is besides the other answer that you got - to "copy always".

3
  • Went through a lot to get the back slashes in. PicPath = "@Images\None.bmp" (this is a correct path for the 'none' image)... Throws the error: "An unhandled exception of type 'System.IO.FileNotFoundException' occured in System.Drawing.dll. Additional information @Images\None.bmp... double-checked. None.bmp DOES exist in both the resource and the folder on disk 'Images'
    – zetar
    Jun 19, 2013 at 15:21
  • 2
    @zetar "@Images\None.bmp" is wrong. @"Images\None.bmp" is correct. The @ just tells C# to not escape the "N" that's after the "\" and rather treat the "\" as simply a backslash. You can also do it with: "\\" (a double backslash.)
    – ispiro
    Jun 19, 2013 at 15:22
  • GOT IT! REMOVED THE @ in the string that was being passed in!
    – zetar
    Jun 19, 2013 at 15:23
19

Ok...so first you need to import the image into your project.

1) Select the PictureBox in the Form Design View

2) Open PictureBox Tasks
(it's the little arrow printed to right on the edge of the PictureBox)

3) Click on "Choose image..."

4) Select the second option "Project resource file:"
(this option will create a folder called "Resources" which you can access with Properties.Resources)

5) Click on "Import..." and select your image from your computer
(now a copy of the image will be saved in "Resources" folder created at step 4)

6) Click on "OK"

Now the image is in your project and you can use it with the Properties command. Just type this code when you want to change the picture in the PictureBox:

pictureBox1.Image = Properties.Resources.MyImage;

Note:
MyImage represent the name of the image...
After typing "Properties.Resources.", all imported image files are displayed...

10

It depends on your file path. For me, the current directory was [project]\bin\Debug, so I had to move to the parent folder twice.

Image image = Image.FromFile(@"..\..\Pictures\"+text+".png");
this.pictureBox1.Image = image;

To find your current directory, you can make a dummy label called label2 and write this:

this.label2.Text = System.IO.Directory.GetCurrentDirectory();
9

The accepted answer has major drawback!
If you loaded your image that way your PictureBox will lock the image,so if you try to do any future operations on that image,you will get error message image used in another application!
This article show solution in VB

and This is C# implementation

 FileStream fs = new System.IO.FileStream(@"Images\a.bmp", FileMode.Open, FileAccess.Read);
  pictureBox1.Image = Image.FromStream(fs);
  fs.Close();
4

Setting "Copy to Output Directory" to "Copy always" or "Copy if newer" may help for you.

Your PicPath is a relative path that is converted into an absolute path at some time while loading the image. Most probably you will see that there are no images on the specified location if you use Path.GetFullPath(PicPath) in Debug.

2
  • I changed to 'Copy always'... the output from Path.GetFullPath(PicPath) is "C:\\Images\\None.bmp" -- Not what I wanted, of course. But, I really don't want the picture to be loaded from the hard drive. Aren't these images available as (what we used to call years ago) 'precompiled inline images'?
    – zetar
    Jun 19, 2013 at 15:00
  • Yes. Sorry a have overseen the leading / as ispiro stated.
    – user1064248
    Jun 20, 2013 at 6:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.