. Net put images and other files into the DLL and reference them in the program

Source: Internet
Author: User

【Abstract]

Sometimes we need to hide some resources in the program, such as the game, before we can see the picture, then the picture must be hidden, otherwise you can see your picture without playing this game.
This article describes how to hide a file (such as a video or Word document) to a DLL, and then export the image as needed in the program.

Note: site original, reproduced please indicate site URL: http://www.beinet.cn/blog/


[Full text]

Step 2:
We need to generate a resource file, first put the file to be hidden into this resource file
(Resource files can basically store three types of data resources: byte arrays, various objects, and strings)

First, create an instance of the class:
ResourceWriter rw = new ResourceWriter ("MyResource. resources"); // file name in brackets

You can use the AddResource () method of the ResourceWriter class to add resources. There are three reloads:

Public void AddResource (string, byte []);
Public void AddResource (string, object );
Public void AddResource (string1, string2 );

The string above is the identifier after the resource file is placed, and the second parameter is the specific resource to be placed.

After a resource is added, you can call the Generate () method of the ResourceWriter class to Generate a resource file.

 

The detailed code is as follows (code name: Res. cs ):

Using System;
Using System. Resources;
Using System. Drawing;

Namespace test {
Class test {
Static void Main (){
Console. Write ("aaa ");
ResourceWriter rw = new ResourceWriter ("MyResource. resources ");

Rw. AddResource ("rsTest", "Heool Word ");

Icon ico = new Icon (". ico ");
Rw. AddResource ("ico", ico );

Image img = Image. FromFile ("3.jpg ");
Rw. AddResource ("img", img );

Rw. Generate ();
}
}
}

Call CSC res. cs to generate an EXE file and run res.exe to run the file to obtain the MyResource. resources file.

Step 2:
The following is the compilation command to embed the generated resource file into the final generated program:

Csc/res: MyResource. Resources/target: winexe yourProgram. CS

Then you can reference the objects in the resource file in WinForm. First:

System. Resources. ResourceManager resMan = new ResourceManager ("MyResource", System. Reflection. Assembly. GetExecutingAssembly ());

Generate a reference to this file. "MyResource" is the file name of the resource file and must use resources as the extension.

Then in the program:

String getFromRS = (string) resMan. GetString ("rsTest"); // obtain the string in the resource file

System. Drawing. Icon GetIcon = (System. Drawing. Icon) resMan. GetObject ("ico"); // obtain the Icon in the resource file

System. Drawing. Image GetIcon = (System. Drawing. Image) resMan. GetObject ("img"); // obtain the Image in the resource file

You can use the content in the resource file directly.
In this way, you can directly add some content or objects to the EXE or DLL to keep them confidential or reduce the number of files.

 

Related Article

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.