wpf 如何找出图像的相对路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14523274/
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
How to find out relative path of image
提问by Mare Infinitus
In a legacy project there are absolute path to the images, for example:
在遗留项目中有图像的绝对路径,例如:
C:/projects/LegacyProject/Project/Client/UserInterface/Images/arrow.png
Now I want to use relative path, so that every developer can use that project, no matter where he has his copy of the sourcecode.
现在我想使用相对路径,这样每个开发人员都可以使用该项目,无论他的源代码副本在哪里。
Is there an easy way to find out the (Resource) relative path? How can I use it then?
有没有一种简单的方法来找出(资源)相对路径?那我怎么用呢?
At the moment I have for example:
目前我有例如:
<Image Source="C:/projects/LegacyProject/Project/Client/UserInterface/Images/arrow.png" Stretch="Fill" />
What I want is something like:
我想要的是这样的:
<Image Source="arrow.png" Stretch="Fill" />
Tried around with
尝试过
<Image Source="pack:,,, arrow.png" Stretch="Fill" />
<Image Source="/WPF1;arrow.png"></Image>
and similar things
和类似的东西
回答by Clemens
Put the image files into a folder (named let's say Images) in your Visual Studio project and set their build actionto Resource.
将图像文件放入ImagesVisual Studio 项目中的一个文件夹(假设命名为),并将它们的构建操作设置为Resource.
Now you can simply use them in XAML like this:
现在您可以像这样在 XAML 中简单地使用它们:
<Image Source="Images/arrow.png" ... />
In code-behind you would have to write
在代码隐藏中,您必须编写
var uri = new Uri("pack://application:,,,/Images/arrow.png");
image.Source = new BitmapImage(uri);
回答by Hossein Narimani Rad
Add image using Sourceproperty of the Imagecontrol by clicking
通过单击使用控件的Source属性添加图像Image


then the path will be something like this:
那么路径将是这样的:
/[project name];component/[folder name: e.g. Images]/[file name]

