无法获得正确的图像路径 [英] Not get correct image path
本文介绍了无法获得正确的图像路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的窗口应用程序中,单击时更改按钮的图像.我使用SaveFileDialog
将数据保存在文本文件中.当我单击按钮将数据保存到文本文件中后,它会很好地产生如下错误:找不到文件异常."如果找不到文件,则在将数据保存到文本文件之前,我不会更改图像.我在按钮单击事件中使用此代码进行图像更改:
In my window application I change image of button when click. I save data in text file using SaveFileDialog
. After save data in text file when I click my button it well generate error like this:"File not found Exception was caught". If file not found then I will not change image before save data in text file. I use this code at button click event for image change:
btn_Stop.Image = Bitmap.FromFile("..\\Image\\btn_Stop.Image.jpg");
btn_Start.Image = Bitmap.FromFile("..\\Image\\btn_Start.red.jpg");
推荐答案
您真的不想在这里使用相对路径-就是这样:相对于当前位置.
如果将这些图像存储在应用程序所在位置下的目录中,请绝对指定该文件夹:
You really do not want to use a relative path here - it is just that: relative to the current location.
If you store these images in a directory below the location where the application resides, then specify that folder absolutely:
btn_Stop.Image = Bitmap.FromFile(Application.StartupPath + "\\Image\\btn_Stop.Image.jpg");
btn_Start.Image = Bitmap.FromFile(Application.StartupPath + "\\Image\\btn_Start.red.jpg");
除了解决方案1,
In addition to the Solution 1,
btn_Stop.Image = Bitmap.FromFile(Path.Combine(Application.StartupPath , "Image\\btn_Stop.Image.jpg"));
btn_Start.Image = Bitmap.FromFile(Path.Combine(Application.StartupPath , "Image\\btn_Start.red.jpg"));
希望对您有所帮助:)
Hope it helps :)
这篇关于无法获得正确的图像路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文