将 Resources.resx 中的 BitmapImage 分配给 Image.Source? [英] Assign BitmapImage from Resources.resx to Image.Source?

查看:29
本文介绍了将 Resources.resx 中的 BitmapImage 分配给 Image.Source?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 Resources.resx 中的 BitmapImage 分配给 Image.事先我将 .png 图像保存到 Resources.resx.此图片现在位于/Resources/logo.png"中.

I would like to assign a BitmapImage from my Resources.resx to an Image. Beforehand I saved a .png image to Resources.resx. This image is now located in "/Resources/logo.png".

在阅读了几篇 SO 帖子和其他文章后,我现在有了这个:

After reading several SO posts and other articles I have this now:

logoImage.Source = new BitmapImage(new Uri(@"pack://application:,,,/Resources/logo.png"));

不幸的是它不起作用.我不知道如何解决这个问题.

Unfortunately it doesn't work. I don't know how to solve this.

我尝试用完整路径替换 Pack-URI 并且它有效,但我想使用相对路径以便在绝对路径不正确的不同机器上使用相同的源.

I tried to replace the Pack-URI with 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.

谁能帮我解决这个问题?

Can anyone help me out with this?

推荐答案

为了使 Uri 正常工作,文件 logo.png 必须包含在 VS 项目中名为Resources"的文件夹中(参见第一张图片),并且它的 Build Action 必须设置为 Resource(见第二张图).

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

这个 Resources 文件夹与 Resources.resx 完全无关.您可以将其重命名为您喜欢的任何名称.

This Resources folder is completely unrelated to Resources.resx. You may rename it to whatever you like.

这篇关于将 Resources.resx 中的 BitmapImage 分配给 Image.Source?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆