7

I am trying to create a Bitmap using:

bitmap = new Bitmap(@"Movies\View\Images\missing_person.bmp"); 

However, I am receiving a System.ArgumentException error.

The file I'm calling the above code from is located in:

MyProj\DisplaySideBarCommand.cs          

The images are in:

MyProj\Movies\View\Images\missing_person.bmp

I also tried using:

bitmap = new Bitmap(@"..\Movies\View\Images\missing_person.bmp"); 

but received the same error.

  • 1
    Can you post the actual error you are getting? `ArgumentException` seems strange. The `Bitmap` constructor should throw a `FileNotFoundException` if the file can't be found. – pstrjds Jan 27 '15 at 01:50
  • 1
    As a debugging tip, try outputting the result of `Path.GetFullPath(@"..\Movies\View\Images\missing_person.bmp")`. That should show you what path it is actually trying to access. – pstrjds Jan 27 '15 at 01:56
  • see this url http://stackoverflow.com/questions/10311433/wpf-dispay-image-from-relative-location http://stackoverflow.com/questions/1444699/wpf-relative-image-source-path – Kishan Jan 27 '15 at 04:21

3 Answers3

3

It is going to look for the files relative to the executing assembly. When you build your project it is probably output to a directory like bin\debug or bin\release. You could build your relative path to backtrack from there, or you could copy the files to the output directory.

If you set the build action to Content on the files, they will be copied to the output directory (including sub folders) on build and then you should be able to build the correct relative path from there.

John Koerner
  • 37,428
  • 8
  • 84
  • 134
0

If you are using the default settings, the debug binaries would be in ProjectDirectory\bin\Debug\ - therefore, you might want to try @"..\..\Movies\View\Images\missing_person.bmp"

0

This is because the images are located in your project folder, not your output folder.

string projectFolder = "..\\..\\"; // Goes back to the project folder

Once you got the projec path simply use it like this:

bitmap = new Bitmap(projectFolder + @"Movies\View\Images\missing_person.bmp");

I would suggest you to move your files to the output folder rather than storing them in your project. Since normally you only distribute the output folder and not your whole project (Unless it's open source of course.)

Bauss
  • 2,767
  • 24
  • 28