28

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
zetar
  • 1,225
  • 2
  • 20
  • 45

5 Answers5

53

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".

ispiro
  • 26,556
  • 38
  • 136
  • 291
  • 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 '13 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 '13 at 15:22
  • GOT IT! REMOVED THE @ in the string that was being passed in! – zetar Jun 19 '13 at 15:23
18

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...

Ola Ström
  • 4,136
  • 5
  • 22
  • 41
Alin Leon
  • 299
  • 3
  • 4
9

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();
chenjesu
  • 734
  • 6
  • 14
8

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();
Samy Massoud
  • 4,295
  • 2
  • 35
  • 48
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.

Bizhan
  • 16,157
  • 9
  • 63
  • 101
  • 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 '13 at 15:00
  • Yes. Sorry a have overseen the leading / as ispiro stated. –  Jun 20 '13 at 06:36