Visual Studio:如何将图像资源存储为嵌入式资源? [英] Visual Studio: How to store an image resource as an Embedded Resource?

查看:526
本文介绍了Visual Studio:如何将图像资源存储为嵌入式资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,当您将图像(图标,位图等)添加为项目资源时,图像的构建操作将设置为。这样做是因为图像神奇地存储在.resources文件中。

By default when you add an image (icon, bitmap, etc.) as a resource to your project, the image's Build Action is set to None. This is done because the image is magically stored inside a .resources file.

希望资源是存储为嵌入式资源(我的理由是无关紧要的,但我们只是假装它,以便我可以在 RedGate的反射器 )。

I want the resource to be stored as an embedded resource (my reasons are irrelevant, but let's just pretend it's so that I can see them inside RedGate's Reflector).

所以我将每个图像的构建操作更改为嵌入式资源,然后资源出现在Lutz的Reflector中 - 完全按照我的意愿。

So I changed each image's Build Action to Embedded Resource, and the resource then appears inside Lutz's Reflector - exactly as I want.

不幸微软特别表示不这样做


注意资源时编辑器
添加了一个图像,它将构建操作设置为
,因为.resx文件
引用了图像文件。在构建
时,图像被拉入
.resx文件中创建的
.resources文件中。然后可以通过为.resx
文件自动生成的强类型
类轻松地访问该图像

Note that when the resource editor adds an image, it sets Build Action to None, because the .resx file references the image file. At build time, the image is pulled into the .resources file created out of the .resx file. The image can then easily be accessed via the strongly-typed class auto-generated for the .resx file.

因此,您不应将
此设置更改为嵌入资源
,因为这样做会包含
图像在程序集中两次。

Therefore, you should not change this setting to Embedded Resource, because doing so would include the image twice in the assembly.

那么正确的方式将图像包含为嵌入式资源?

So what is the proper way to include an image as an embedded resource?

推荐答案

注意:此答案不是处理图像资源的推荐方法。它只是解决问题所描述的特定问题(即将图像包含为嵌入式资源)。

Note: This answer is not the recommended way of handling image resources. It just addresses the particular problem as described by the question (i.e. to include an image as an embedded resourse).

不要添加图像作为资源。我宁愿做以下事情:

Don't add the image as a resource. I would rather do the following:


  • 创建图像/图标并将其保存到文件

  • 选择项目 - >添加现有项目并添加文件

  • 将构建操作设置为嵌入资源

然后,您可以使用

Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceUri)

这样,图像神奇地添加到项目资源文件中,您只有一个副本存储在程序集资源中的图像。

This way the image is not magically added to the projects resource file and you will only have one copy of the image stored in the assembly's resources.

这篇关于Visual Studio:如何将图像资源存储为嵌入式资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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