C# 来自相对资源的 ImageSource
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12631199/
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
ImageSource from a relative resource
提问by 00jt
I'm trying to use a local resource to put an icon inside a Button. (C# Visual Studio 2012)
我正在尝试使用本地资源将图标放在 Button 中。(C# Visual Studio 2012)
Directly inside my project i have a folder called "Resources" which contains "Icons/MyIcon.png"
直接在我的项目中,我有一个名为“Resources”的文件夹,其中包含“Icons/MyIcon.png”
The following code works (but is not using a relative path)
以下代码有效(但不使用相对路径)
<Button Width="20" Height="20">
<Button.Background>
<ImageBrush ImageSource="C:\Documents\MyProject\Resources\Icons\MyIcon.png">
</ImageBrush>
</Button.Background>
However, i want it to use a relative path... so i tried the following
但是,我希望它使用相对路径......所以我尝试了以下
<ImageBrush ImageSource="..\MyProject\Resources\Icons\MyIcon.png">
It compiles, but then gives me the error:
它编译,但随后给了我错误:
{"Cannot locate resource 'myproject/resources/icons/myicon.png'."}
Then i found an article that talked about referencing the project and said to do:
然后我找到了一篇关于引用项目的文章,并说要做:
"/[ assembly name ];component[ path within the project ]"
so i tried that:
所以我试过:
<ImageBrush ImageSource="/MyProject;component\Resources\Icons\MyIcon.png">
But it doesn't work.
但它不起作用。
It DOES show up in the GUI-Builder... but when i actually try to "debug" the code... it fails to run.
它确实出现在 GUI-Builder 中……但是当我实际尝试“调试”代码时……它无法运行。
If i change the path at all by adding '123' for example:
如果我通过添加“123”来更改路径,例如:
<ImageBrush ImageSource="/MyProject;component\Resources\Icons\MyIcon123.png">
i get a pre-compile error: "cannot find the file specified". Once i remove the '123' the error goes away. And the icon again is displayed in the Form. But when i run... i still get:
我收到一个预编译错误:“找不到指定的文件”。一旦我删除了“123”,错误就会消失。图标再次显示在表单中。但是当我跑步时......我仍然得到:
{"Cannot locate resource 'resources/icons/myicon.png'."}
What is going on? Why can it only find the resource BEFORE it runs? and not when i actually start?
到底是怎么回事?为什么它只能在运行之前找到资源?而不是当我真正开始?
采纳答案by Eric Olsson
Do you need to set the BuildAction of the image to Resource? Is it being built into the assembly? Or are you treating it like Content? In that case, you'd want to set the BuildAction to Content and set CopyToOutputDirectory appropriately.
需要把图片的BuildAction设置为Resource吗?它是否被内置到程序集中?或者你把它当作内容来对待?在这种情况下,您需要将 BuildAction 设置为 Content 并适当地设置 CopyToOutputDirectory。
Here'sa link to an MSDN article treating Resources, Content files, etc.
这是一篇关于处理资源、内容文件等的 MSDN 文章的链接。
回答by N_A
I covered this in a blog post here.
It basically looks like this for a folder named "Images". If your .xaml file is in a different folder then you need to do ../Images/favicon.ico. Notice that in neither case do you need to add the project name to the start of the path. If you need more details see the example project at the end of the post.
对于名为“Images”的文件夹,它基本上看起来像这样。如果您的 .xaml 文件位于不同的文件夹中,那么您需要执行../Images/favicon.ico. 请注意,无论哪种情况,您都不需要将项目名称添加到路径的开头。如果您需要更多详细信息,请参阅文章末尾的示例项目。
<Image Margin="5" Source="/Images/favicon.ico" Height="100"/>
回答by user1664384
This worked for me for relative path for an image whose resource folder is in the Application.
这对我来说适用于资源文件夹在应用程序中的图像的相对路径。
<Image Source="..\Images\down_arrow.gif" />
回答by J. Henry Slentz
I know this is an old post but for VS2013 none of these examples worked for me but a combination of all of them is how I now have to reference my image resources & sources within a URI..
我知道这是一篇旧帖子,但对于 VS2013,这些示例都不适用于我,但是所有这些示例的组合是我现在必须在 URI 中引用我的图像资源和来源的方式。
"pack://application:,,,/!APP NAME!;component/Resources/Purple.bmp"
Of course substitute !APP NAME!for yours.. "pack://application:,,,/test1;component/Resources/Purple.bmp"
当然可以代替!APP NAME!你的。。"pack://application:,,,/test1;component/Resources/Purple.bmp"

