0

Ok let me explain it again My problem is I want to display the image. But i want this without the opendialogfile I tried this:

        pictureBox1.Image = Image.FromFile("C:\\Users\\Abdullah\\Documents\\Visual Studio 2013\\Projects\\Maker\\Maker\\add.png");// it works

But i dont want to do this because it will cause errors at deployment time. What i want to do is:

pictureBox1.Image= Image.FromFile("add.png");// because this picture is already in the project folder

In this case it show me error that file not found Now Hope so I explained it :)

  • 4
    Hard to decode this question. You always get the *full* path of the file from the dialog. So your code can never fail. A path like "Images" is a fail whale, it depends entirely too much on Environment.CurrentDirectory – Hans Passant Sep 20 '14 at 08:59
  • Could you explain better what is your problem? As now you set the InitialDirectory and this will be the path shown initially by the OpenFileDialog (**if it exists**). For the _root_ part then do you want to extract the parent folder of the full filename selected by your user? – Steve Sep 20 '14 at 09:25
  • That image is usually shown when the image file doesn't exist in that location. Please check. – Steve Sep 20 '14 at 10:48

1 Answers1

1

Assuming that you are hard coding the path to your image and the image really exists in that path, then you should remember to escape the backslash when using a constant string like that one.

Try with

 pictureBox1.ImageLocation = @"C:\Users\Abdullah\Documents\Visual Studio 2013
                               \Projects\Maker\Maker\Resources\add.png";

or

 pictureBox1.ImageLocation = "C:\\Users\\Abdullah\\Documents\\Visual Studio 2013
                                \\Projects\\Maker\\Maker\\Resources\\add.png";

(Warning strings splitted in two lines for readability. It should be on one single line)

See How do I write a backslash?

EDIT: Based on your comment below, then it seems that the Image folder always exists in your project (also when it will be deployed to a customer machine) then you could write something like this

 pictureBox1.ImageLocation = Path.Combine(Application.StartupPath, "Images", "add.png");

or

string imageFile = Path.Combine(Application.StartupPath, "Images", "add.png");
pictureBox1.Image= Image.FromFile(imageFile);

But looking back to your example: Is it Images or Resources?

Community
  • 1
  • 1
Steve
  • 213,761
  • 22
  • 232
  • 286
  • I am really sorry sir to say you that it works but i want to eliminate the previous whole string. I just want to write picturebox1.ImageLocation ="Images/add.png" because the images folder exists with in the project folder – Muhammad Nasir Khan Sep 20 '14 at 10:54
  • See the updated answer. The Path and Application classes are very helpful to remove hard coded paths in your application – Steve Sep 20 '14 at 11:02
  • pictureBox1.ImageLocation = Path.Combine(Application.StartupPath, "Images", "add.png"); This works but does not displays the image. It just displays a cross symbol at the form nothing else (I want to pick the images from the Images Folder. Yes before that it was in the resources folder but now i switched them to the Images folder) – Muhammad Nasir Khan Sep 20 '14 at 11:05
  • Sorry but I don't understand in which location is this Images folder. If it is relative to the current startup folder then the code above search the file add.png there. If instead you talk about the Operating System folder for Images then its path could be retrived using then Environment.SpecialFolder enum – Steve Sep 20 '14 at 22:15