DLL embedded image resources

Source: Internet
Author: User
Document directory
  • Attach image resources
  • Resource path:
  • Show all embedded Resources

If you use images in the net program, you will find it much easier to embed images into a resource file than to use them as separate files!

First, add image resources:

Add images to your project as an embedded Resource:
1. in Visual Studio, clickProject
Menu, and then selectAdd existing item
. Find and select the image you want to add to the project.
2. In Solution Explorer, right-click the image you just added to the project and selectProperties
The property toolbar is displayed.
3. In this toolbar (for example), changeBuild action
The property isEmbedded Resource
.
4. regenerate the project. This image will be compiled into the program set of your project.

Attach image resources

Make sure to add a reference to your project:

Using
System. IO;
Using
System. reflection;


To load this image resource in the program, use the following code:

1
Assembly myassembly
=
Assembly. getexecutingassembly ();

2
Stream mystream
=
Myassembly. getmanifestresourcestream (
"
Mynamespace.subfolder.myimage.bmp
"
);

3
Bitmap BMP
=
 
New
Bitmap (mystream); resource path:

The correct path format is as follows:

<Namespace>. <subfolders>. <image name>. <extension> (
<Namespace>. <folder>. <image name>. <suffix>
)

Description
:

Namespace
Is the project namespace.

Extension
The format of the image (for example, "BMP" or "jpg ").
Tip: Not like
Windows file path. The embedded resource path is based on the absolute path.

For example, the about24.png image file is saved in the art/a folder of the main program. In this case, the path will be "mynamespace.art.a.about24.png ".

Show all embedded Resources

If you have trouble determining the correct path, you can use the following code to display all the embedded resources in this path:

 

1
Assembly myassembly
=
Assembly. getexecutingassembly ();

2
String
[] Names
=
Myassembly. getmanifestresourcenames ();

3
Foreach
(
String
Name
In
Names)

4
{

5
Console. writeline (name );

6
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.