Image.FromFile 将打开一个路径,但不会打开包含相同路径的字符串?
// 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;
当我单击应该更改图片框图像的按钮时,我收到一个错误,基本上是路径中的非法字符,并且ArgumentException未处理
抱歉,第一次没有这样做时间。
k 因此实际的文件名被输入到文本框中。然后,我将该文本转换为字符串,并将其添加到开头和末尾以创建完整的文件路径。
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.");
}
// 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.");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只是猜测,但如果文本框确实显示:
然后你的路径中有引号,这很糟糕。在您的代码中,您使用引号来定义字符串,如果您要获取用户的输入,则不需要引号。
Just a guess, but if the text box literally displays:
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.
这两行应该都可以正常工作,所以显然,您的代码正在做的事情并不是您所认为的。
我看到您使用文本框来检查变量的值?帮自己一个忙,学习:
Diagnostics.Trace.WriteLine()
和如何使用调试器。
(最重要的是调试器。)然后您将能够找出代码出了什么问题。因为从你向我们展示的情况来看,不可能发现它有什么问题。
--哦,是的,请下次您在这里发布一些内容告诉我们“它不起作用”时,请告诉我们它到底在什么方面不起作用。它默默地什么都不做吗?它会抛出异常吗?它是否加载了与您期望的图像不同的图像?它会崩溃并燃烧吗?这是一点必要的。
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:
Diagnostics.Trace.WriteLine()
andHow 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.
你的斜线是错误的。尝试将其设为逐字字符串,即:
@"C:\Users\Admin\Desktop\apps\pic1.png"
Your slashes are the wrong way around. Try making it a Verbatim string, ie:
@"C:\Users\Admin\Desktop\apps\pic1.png"
更改路径,
编辑:路径包含无效字符。
Change the path,
EDIT: Path contains invalid character.