How to get image from a folder path in wpf

戏子无情 提交于 2019-12-20 02:48:06

问题


I'm trying to understand how to make a get a file from my project I'm using visual studio 2010, and I have a folder in my wpf project that is called: Images

How can I get an image from that folder, I'am trying:

return @"/Images/"+ name+".jpg";

the folder it self is in : C:\Users\Boaz-Pc\Documents\Visual Studio 2010\Projects\FinalSadna\FinalSadna\Images

But it doesnt work for me. Any ideas?


回答1:


you can look at something like this

string imageName = name + ".jpg";
var path = Path.Combine(GetApplicationFolder(), "/Images/" + imageName);

if you want to find it via the Assembly you could use the following as well

var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);



回答2:


what about

var uriSource = new Uri(@"/FinalSadna;component/imagename.png", UriKind.Relative);



回答3:


I think your problem is that the relative path, at least from the code we can see, is relative to the output exe file. I doubt this folder is bin>debug or bin>release. So you would need to go back a certain number of directories (hopes). This article explains relative paths very well.

EDIT:

Unless you have custom compiling option enabled, then you are incorrectly referencing the relative path. Have a look the above link, please.

Hope this helps you!



来源:https://stackoverflow.com/questions/14308846/how-to-get-image-from-a-folder-path-in-wpf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!