Reputation: 256751
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.
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).
So I changed each image's Build Action to Embedded Resource, and the resource then appears inside Lutz's Reflector - exactly as I want.
Unfortunately, Microsoft says specifically not to do this:
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?
Upvotes: 25
Views: 76096
Reputation: 61233
In the project view, open the Properties subtree; double-click the Resources.resx item.
(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.
Click the Save icon.
You can now access your embedded resource programmatically as:
[namespace].Properties.Resources.[yourResourceName]
Upvotes: 16
Reputation: 176179
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:
You can then access this resource using
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.
Upvotes: 43
Reputation: 704
In VS 2005:
To reference the image in code use something like:
btnStop.Image = myImages.Stop;
Upvotes: 6
Reputation: 5117
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 ResourceManager
is a good way to go because it provides some nice functionality for satellite assemblies, searching, fallbacks, etc.
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 Resource
is fine.
The documentation warns you about not setting the build action to Embedded Resource
because the resource is already compiled into the assembly by the resource compiler via the .resx file.
Upvotes: 5