How to read various resource under. NET

Source: Internet
Author: User

1) Embedded Resource (Build Action set to Embedded Resource) uses GetManifestResourceStream read at run time

Image.fromstream (assembly.getexecutingassembly (). GetManifestResourceStream ("WindowsFormsApplication1.Properties.Resources.warning.png"));

2) Resource (Build Action set to Resource) is read at run time using Resource Manager

ResourceManager rm = new ResourceManager ("Windowsformsapplication1.g", typeof (Form1). Assembly);

Image warimg = null;
ResourceSet rs= RM. GetResourceSet (New System.Globalization.CultureInfo ("en"), true,true);
foreach (DictionaryEntry item in RS)
{
Console.WriteLine (item. Key.tostring ());

warimg = Bitmap.fromstream (item. Value as Stream);

}

3) If you are adding pictures directly to. ResX resource file, read with Resource manager at run time, but read differently

ResourceManager rm = new ResourceManager ("WindowsFormsApplication1.Properties.Resources", typeof (Form1). Assembly);

Bitmap warnimg = rm. GetObject ("Warning") as Bitmap;

The above is the case under WinForm and WPF, and there is another way of embedding resources under ASP. Webresourceattribute, Note: The build action for image here must be embedded Resource.

Add assembly attribute:

[Assembly:webresourceattribute ("ServerControl1.images.component.gif", "Image/gif")]

Client reads:

String imgurl = Page.ClientScript.GetWebResourceUrl (typeof (ServerControl1), ("ServerControl1.images.component.gif" );

How to read various resource under. NET

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.