C# 将 Resources.resx 中的 BitmapImage 分配给 Image.Source?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12690774/
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
Assign BitmapImage from Resources.resx to Image.Source?
提问by TorbenJ
I would like to assign a BitmapImagefrom my Resources.resx to an Image.
Beforehand I saved a .png image to Resources.resx. This image is now located in "/Resources/logo.png".
我想将BitmapImageResources.resx 中的a 分配给Image. 事先我将 .png 图像保存到 Resources.resx。该图像现在位于“/Resources/logo.png”中。
After reading several SO posts and other articles I have this now:
在阅读了几篇 SO 帖子和其他文章后,我现在有了这个:
logoImage.Source = new BitmapImage(new Uri(@"pack://application:,,,/Resources/logo.png"));
Unfortunately it doesn't work. I don't know how to solve this.
不幸的是它不起作用。我不知道如何解决这个问题。
I tried to replace the Pack-URIwith the fullpath and it worked but I would like to use relative paths in order to use the same source on different machines on which the absolute path would be incorrect.
我试图Pack-URI用完整路径替换并且它有效,但我想使用相对路径以便在绝对路径不正确的不同机器上使用相同的源。
Can anyone help me out with this?
谁能帮我解决这个问题?
采纳答案by Clemens
In order to make that Uri work, the file logo.png must be contained in a folder named "Resources" in your VS project (see first image), and its Build Action must be set to Resource (see second image).
为了使该 Uri 工作,文件 logo.png 必须包含在 VS 项目中名为“Resources”的文件夹中(参见第一张图片),并且其构建操作必须设置为 Resource(参见第二张图片)。




This Resources folder is completely unrelated to Resources.resx. You may rename it to whatever you like.
此资源文件夹与 Resources.resx 完全无关。您可以将其重命名为您喜欢的任何名称。

