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
}