為了更方便,如果您的資源和加載資源的類碰巧使用了相同的命名空間,則可以將類的類型作為可選的第一參數傳遞給 GetManifestResourceStream:
namespace ResourcesApp {
public class Form1 : Form {
public Form1() {
...
// Get this type’s assembly
Assembly assem = this.GetType().Assembly;
// Load the resource using a namespace
// Will load resource named "ResourcesApp.Azul.jpg"
Stream stream =
assem.GetManifestResourceStream(this.GetType(), "Azul.jpg");
// Load the bitmap from the stream
this.BackgroundImage = new Bitmap(stream);
}
...
}
}
GetManifestResourceStream 將使用如下格式編寫資源名稱:
<namespace>.<fileName>
在加載某些類型(比如 Bitmap 類)時,使用類型和文件名也是有用的,這樣可以通過提供構造函數避免由您自己打開流:
namespace ResourcesApp { public class Form1 : Form { public Form1() { ... // Get this type’s assembly Assembly assem = this.GetType().Assembly; // Load the bitmap directly from the manifest resources this.BackgroundImage = new Bitmap(this.GetType(), "Azul.jpg"); } ... }}
返回頁首
Visual Studio .NET 中的清單資源
如果(大多數情況下)您使用 Visual Studio?NET 來開發和構建程序集,則用命令行嵌入清單資源的方法不可能非常吸引您。這種情況下,您可以將資源添加到 Windows 窗體項目中,該方法將把合適的命令行參數傳遞給編譯器。
要將資源添加到項目中,請在 Solution Explorer 中右鍵單擊項目,然后選擇 Add New Item,并選擇您想作為資源嵌入的文件。文件將復制到項目的目錄中,但仍然不會被嵌入。要使文件作為資源嵌入,請右鍵單擊文件,并選擇 Properties,然后將 Build Action 從 Content(默認)更改為 Embedded Resource,如圖 3 所示。
文章來源于領測軟件測試網 http://www.kjueaiud.com/