C#——在资源dll外获取其中的资源文件、图片等等

来源:互联网 发布:winpe ghost网络克隆 编辑:程序博客网 时间:2024/05/29 04:48

(1)该资源dll(resDll)的编译,在引入图片文件后(在Resources目录下),右击:属性:生成的操作:嵌入资源

 

(2)在另外的文件中引入该资源文件,然后以下程序即可进行调用:

        using System.Reflection;

           Assembly myAssembly;            
            myAssembly = System.Reflection.Assembly.Load("resDll");         
            System.Resources.ResourceManager myManager = new System.Resources.ResourceManager("resDll.Properties.Resources", myAssembly);              
            System.Drawing.Image myImage= (System.Drawing.Image)myManager.GetObject("HW");  //HW.BMP

 

(3)    或者在带来了内定义公共函数

 

 ///从本dll中提取名称为str.bmp的bmp文件
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
         public System.Drawing.Image getImage(string str)
         {

             Assembly asm = Assembly.GetExecutingAssembly();
             string name = asm.GetName().Name;
             Bitmap bmp = new Bitmap(asm.GetManifestResourceStream(name + ".Resources."+str)); 
             return bmp;

         }
           
     其他程序中使用图片,只需

  resDll.res a =new resDll.res();

pictureBox1.Image = a.getImage("HW.bmp");  

0 0