. NET to put pictures and other files into the DLL, and reference in the program

Source: Internet
Author: User

Summary

Sometimes we need to hide some resources in the program, such as the game, after the clearance to see the picture, then the picture must be hidden, otherwise do not play this game can see your picture, hehe.
This article describes how to hide files (compared to tablets, Word documents, etc.) in the DLL, and then in the program you can export pictures as needed for processing.

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


Full-text

1th Step:
We want to generate a resource file, first put the hidden files into this resource file
(Resource files can hold approximately three kinds of data resources: byte arrays, various objects, and strings)

First create an instance of a class:
ResourceWriter rw = new ResourceWriter ("myresource.resources");//file name in parentheses

Add a resource with the AddResource () method of the ResourceWriter class with three overloads:

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

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

Once the resource is added, the Generate () method of the ResourceWriter class is called to produce 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");
 &n bsp;    RW. AddResource ("ico", ICO);
      
      Image img = image.fromfile ("3.jpg");
      RW. AddResource ("IMG", IMG);
      
      RW. Generate ();
   }
 }

Call CSC res.cs, generate EXE file, then run Res.exe execution, you can get myresource.resources file.

2nd Step:
The following is to embed the generated resource file into the last generated program, embedding the program's compile command:

Csc/res:myresource.resources/target:winexe YourProgram.CS

You can then refer to the object in the resource file in WinForm, first:

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

To generate a reference to this file, "MyResource" is the file name of the resource file and must be extended with resources.

Then in the program:

String Getfromrs = (string) resman.getstring ("Rstest"); Gets the string in the resource file

System.Drawing.Icon GetIcon = (System.Drawing.Icon) resman.getobject ("ico"); Get the icon in the resource file

System.Drawing.Image GetIcon = (System.Drawing.Image) resman.getobject ("img"); Get a picture from a resource file

Remove the contents of the resource file and you can use it directly.
This can be used to attach some content or objects directly to the EXE or DLL, to achieve confidentiality, or reduce the number of files.

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.