Sunday, March 27, 2011

Visual Studio: How to store an image resource as an Embedded Resource?

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?

From stackoverflow
  • 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.

    Ian Boyd : But how do you set the Build Action to Embedded Resource, and not have it double included?
  • 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

    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.

    Aaron Palmer : Why is this answer voted down?
    Ian Boyd : This sounds like a good answer, but i'll have to wait to test it before i can accept it.
    Kyralessa : The disadvantage to this solution is that you won't get the autogenerated name for the resource (e.g., in VB .NET, My.Resources.NameOfYourImage. But if that doesn't bother you, then it's probably the best solution.
    0xA3 : But since the OP definitely wants the resource stored as an Embedded Resource, I guess that he has to access it using the GetManifestResourceStream methods
  • In 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.resx and 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.

    To reference the image in code use something like:

    btnStop.Image = myImages.Stop;
    
  • 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]
    

0 comments:

Post a Comment