Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
387 views
in Technique[技术] by (71.8m points)

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...