zoukankan      html  css  js  c++  java
  • .NET 下各种Resource的读取方式

    1) Embedded Resource (Build Action 设置为 Embedded Resource) 在运行时使用GetManifestResourceStream读取

    Image.FromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream("WindowsFormsApplication1.Properties.Resources.warning.png"));

    2) Resource (Build Action 设置为 Resource) 在运行时使用 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) 如果是直接添加图片到.Resx资源文件中,在运行时使用Resource Manager读取, 但读取方式有不同

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

                Bitmap warnImg = rm.GetObject("warning") as Bitmap;

    以上是WinForm 和 WPF下的情况,在ASP.NET下还有另外一种嵌入资源方式,通过WebResourceAttribute, NOTE: 这里的image的build action必须是Embedded Resource.

    添加 assembly attribute:

    [assembly: WebResourceAttribute("ServerControl1.images.component.gif", "image/gif")]

    客户端读取:

    string imgURL = Page.ClientScript.GetWebResourceUrl(typeof(ServerControl1), ("ServerControl1.images.component.gif");

  • 相关阅读:
    Android P Beta发布!最新版本抢先体验!
    手游热更新方案--Unity3D下的CsToLua技术
    2018 Unite大会——《使用UPA工具优化项目》演讲实录
    浅谈软件工程师的代码素养
    Android平台的Swift—Kotlin
    1计算机的基本组成-3
    1计算机的基本组成-2
    新的公司
    4 对象的行为 方法操作实例变量
    反射机制
  • 原文地址:https://www.cnblogs.com/xixifusigao/p/2360903.html
Copyright © 2011-2022 走看看