Put images and other files in the. NET file into the DLL.

Source: Internet
Author: User
Sometimes we need to hide Program Some of the resources, such as the game, can only see the picture after the pass, 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.

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 names in parentheses

use the addresource () method of the resourcewriter class to add resources. There are three reloads:
Public void addresource ( string , byte []);

 Public VoidAddresource (String,Object);

Public VoidAddresource (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.

Then you can reference the objects in the resource file in winform. First:
DetailsCodeThe Code is as follows (res. CS ):
UsingSystem;

UsingSystem. Resources;

UsingSystem. drawing;

NamespaceTest {

ClassTest

{

Static VoidMain ()

{

Console. Write ("Aaa");

Resourcewriter RW= NewResourcewriter ("Myresource. Resources");

RW. addresource ("Rstest","Heool word");

Icon ICO= NewIcon (". 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 system. Resources. ResourceManager resman= NewResourceManager ("Myresource", System. reflection. Assembly. getexecutingassembly ());

Generate a reference to this file,"Myresource"Is the name of the resource file, which must use the resource 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 system. Drawing. Image getIcon = (system. Drawing. Image) resman. GetObject ("IMG") in the resource file "); // Retrieve images from resource files

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.

the Article is reproduced in dezi studio: http: /// www.dezai.cn/article_show.asp? ArticleID = 17304

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.