visual-studio Visual Studio:如何将图像资源存储为嵌入式资源?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/278838/
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
Visual Studio: How to store an image resource as an Embedded Resource?
提问by Ian Boyd
By default when you add an image (icon, bitmap, etc.) as a resource to your project, the image's Build Actionis set to None. This is done because the image is magically stored inside a .resources file.
默认情况下,当您将图像(图标、位图等)作为资源添加到项目时,图像的Build Action设置为None。这样做是因为图像神奇地存储在 .resources 文件中。
Iwant 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).
我希望将资源存储为嵌入式资源(我的原因无关紧要,但让我们假设它是这样我可以在RedGate 的 Reflector 中看到它们)。
So I changed each image's Build Actionto Embedded Resource, and the resource then appears inside Lutz's Reflector - exactly as I want.
因此,我将每个图像的Build Action更改为Embedded Resource,然后该资源出现在 Lutz 的 Reflector 中 - 完全符合我的要求。
Unfortunately, Microsoft says specifically not to do this:
不幸的是,微软明确表示不要这样做:
Note that when the resource editor adds an image, it sets Build Actionto 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.
请注意,当资源编辑器添加图像时,它会将Build Action设置为 None,因为 .resx 文件引用了图像文件。在构建时,图像被拉入从 .resx 文件创建的 .resources 文件中。然后可以通过为 .resx 文件自动生成的强类型类轻松访问该图像。
因此,您不应将此设置更改为Embedded Resource,因为这样做会在程序集中两次包含图像。
So what is the proper wayto include an image as an embedded resource?
那么将图像包含为嵌入资源的正确方法是什么?
回答by Dirk Vollmar
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:
不要将图像添加为资源。我宁愿做以下事情:
- Create the image/icon and save it to a file
- Choose Project -> Add Existing Item and add the file
- Set the Build Action to Embedded Resource
- 创建图像/图标并将其保存到文件中
- 选择 Project -> Add Existing Item 并添加文件
- 将构建操作设置为嵌入式资源
You can then access this resource using
然后,您可以使用访问此资源
Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceUri)
This way the image is notmagically added to the projects resource file and you will only have one copy of the image stored in the assembly's resources.
这样,图像不会神奇地添加到项目资源文件中,并且您将只有一个图像副本存储在程序集的资源中。
回答by Steven A. Lowe
In the project view, open the Properties subtree; double-click the Resources.resx item.
在项目视图中,打开 Properties 子树;双击 Resources.resx 项。
(If you don't have one, right-click the Properties item and choose Open, then click on the Resources tab on the left, and click the link in the middle of the page to create a default resources file.)
(如果没有,请右键单击“属性”项并选择“打开”,然后单击左侧的“资源”选项卡,然后单击页面中间的链接以创建默认资源文件。)
click the first droplist at the top of the resources.resx page. It probably says something unhelpful like "Strings", and select Images. Alternately you can press Ctrl+ 2. This changes to the Image resources view. Now click the Add Resource droplist and choose 'existing file'. Select your image file and click Open. Rename the resource if you like.
单击 resources.resx 页面顶部的第一个下拉列表。它可能会说一些无用的东西,比如“字符串”,然后选择图像。或者,您可以按Ctrl+ 2。这将更改为图像资源视图。现在单击“添加资源”下拉列表并选择“现有文件”。选择您的图像文件,然后单击打开。如果您愿意,可以重命名资源。
Click the Save icon.
单击保存图标。
You can now access your embedded resource programmatically as:
您现在可以通过以下方式以编程方式访问您的嵌入式资源:
[namespace].Properties.Resources.[yourResourceName]
回答by MBoy
In VS 2005:
在 VS 2005 中:
- Right click on your project and choose add new item.
- Add a resource file. Call is
myImages.resx
Place this file in the root folder of the project. - Right click on myImages.resxand choose View Designer
- Select Add Resource, Add Existing file.
- Browse for the image e.g. stop.bmp
This image does not have to be included in the project at this stage. The resource file will automatically do this.
- 右键单击您的项目并选择添加新项目。
- 添加资源文件。调用是
myImages.resx
把这个文件放在项目的根文件夹中。 - 右键单击myImages.resx并选择查看设计器
- 选择添加资源、添加现有文件。
- 浏览图像,例如stop.bmp
此图像在此阶段不必包含在项目中。资源文件将自动执行此操作。
To reference the image in code use something like:
要在代码中引用图像,请使用以下内容:
btnStop.Image = myImages.Stop;
回答by Todd
This depends on how you want to use the image.
这取决于您想如何使用图像。
If you want to localize and access specific images for a specific culture, then using the ResourceManageris a good way to go because it provides some nice functionality for satellite assemblies, searching, fallbacks, etc.
如果您想本地化和访问特定文化的特定图像,那么使用ResourceManager是一个很好的方法,因为它为附属程序集、搜索、回退等提供了一些不错的功能。
If you just want to embed a resource in an assembly and aren't worried about localization (using Assembly.GetManifestResourceStream) then just adding a image and setting the build action to Embedded Resourceis fine.
如果您只想在程序集中嵌入资源并且不担心本地化(使用Assembly.GetManifestResourceStream),那么只需添加图像并将构建操作设置为即可Embedded Resource。
The documentation warns you about not setting the build action to Embedded Resourcebecause the resource is already compiled into the assembly by the resource compiler via the .resx file.
该文档警告您不要将构建操作设置为,Embedded Resource因为资源编译器已通过 .resx 文件将资源编译到程序集中。

