1
// The following line works.
imagebox.Image = Image.FromFile("C:/Users/Admin/Desktop/apps/pic1.png");

// The following line does not work.
imagebox.Image = Image.FromFile(imgPath);

// the test Text Box displays "C:/Users/Admin/Desktop/apps/pic1.png", exactly like in the first line
test.Text = imgPath;

When i click the button that is supposed to change the picturebox's image, i get an error basically saying illegal characters in path, and ArgumentException was unhandled

sorry for not doing that the first time.

k so the actual filename is being entered into a text box. I'm then converting that text into a string, and adding it to the begging and end to create a full file path.

string path = "\"C:/Users/Admin/Desktop/apps/";
string ext1 = ".png\"";


ID = idBox.Text;
imgPath = path + ID + ext1;
try
{
    imagebox.Image = Image.FromFile(imgPath);
}
catch (System.IO.FileNotFoundException)
{
    MessageBox.Show("Invalid Student or Faculty ID.");
}
zck17
  • 19
  • 1
  • 3

4 Answers4

2

Just a guess, but if the text box literally displays:

"C:/Users/Admin/Desktop/apps/pic1.png"

Then you have quotes in your path, which is bad. In your code you use the quotes to define a string, if you're grabbing input from a user you don't need the quotes.

Eric Rahm
  • 688
  • 3
  • 5
1

Both lines should work just fine, so obviously, what your code is doing is not what you think it is doing.

I see you use text boxes to examine the values of your variables? Do yourself a favor and learn:

  1. Diagnostics.Trace.WriteLine() and

  2. How to use the debugger.

(Most importantly the debugger.) You will then be able to figure out what is wrong with your code. Because from what you have shown us, it is impossible to find anything wrong with it.

--Oh yes, and please, next time you post something here telling us "it does not work", please tell us exactly in what way it does not work. Does it silently do nothing? Does it throw an exception? Does it load an image other than the one you were expecting? Does it crash and burn? It is a tad essential.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
  • When i click the button that is supposed to change the picturebox's image, i get an error basically saying illegal characters in path, and ArgumentException was unhandled.. – zck17 Jan 12 '12 at 02:18
  • @user1144513: How are you setting the imgpath variable? Are you sure it is not using backslashes instead of forward slashes (and they are not being escaped like this \\\)? – Jason Down Jan 12 '12 at 02:21
  • Thank you. Please put that in your question so that others will see it too. – Mike Nakis Jan 12 '12 at 02:21
  • Try using @"C:\Users\Admin\Desktop\apps\pic1.png" – Mike W Jan 12 '12 at 02:21
  • Mike W, imagebox.Image = Image.FromFile("C:/Users/Admin/Desktop/apps/pic1.png"); works fine, it only crashes and reports the error when I have "C:/Users/Admin/Desktop/apps/pic1.png" in a string. if there is a way to add the @ verbatim literal in the string please explain – zck17 Jan 12 '12 at 02:23
0

Change the path,

string imgPath=@"c:\Users\Admin\Desktop\apps\pic1.png";

EDIT: Path contains invalid character.

string path = @"C:\Users\Admin\Desktop\apps\";
string ext1 = ".png";
ID = idBox.Text;
imgPath = path + ID + ext1;
if(System.IO.File.Exists(imgPath))
   imagebox.Image = Image.FromFile(imgPath);
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
  • 1
    Yes, but we still do not know why he is receiving that error. – Mike Nakis Jan 12 '12 at 02:23
  • @user1144513 - Please show us **how imgPath var is assigned**. – KV Prajapati Jan 12 '12 at 02:28
  • k well that is going to take a bit more explanation. I'll put it in the original question – zck17 Jan 12 '12 at 02:29
  • thank you a lot but Eric Rahm fixed it by simply taking out the \" i had at the beginning and end of the path. The quotation marks were causing it to fail. I did not realize they were just defining a string. can you explain why the if statement instead of the try-catch statement is better? – zck17 Jan 12 '12 at 02:39
0

Your slashes are the wrong way around. Try making it a Verbatim string, ie:

@"C:\Users\Admin\Desktop\apps\pic1.png"

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321