C#——在資源dll外獲取其中的資原始檔、圖片等等
(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");