wpf 如何从wpf中的文件夹路径获取图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14308846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:00:36  来源:igfitidea点击:

How to get image from a folder path in wpf

c#wpfpath

提问by Daerik Fisher

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

我正在尝试了解如何从我的项目中获取文件我正在使用 Visual Studio 2010,并且我的 wpf 项目中有一个名为: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

它自己所在的文件夹: C:\Users\Boaz-Pc\Documents\Visual Studio 2010\Projects\FinalSadna\FinalSadna\Images

But it doesnt work for me. Any ideas?

但它对我不起作用。有任何想法吗?

回答by MethodMan

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);

回答by Mustafa Ekici

what about

关于什么

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

回答by FrostyFire

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). Thisarticle explains relative paths very well.

我认为你的问题是相对路径,至少从我们可以看到的代码来看,是相对于输出 exe 文件的。我怀疑这个文件夹是 bin>debug 还是 bin>release。所以你需要返回一定数量的目录(希望)。这篇文章很好地解释了相对路径。

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!

希望这对你有帮助!