更新:2007 年 11 月
這個範例擷取屬於組件內嵌資源的影像。
程序
若要設定本範例
建立具有 PictureBox 控制項 (名為 pictureBox1) 的 Windows Form 應用程式。
將下列程式碼範例加入至 Form1_Load 事件處理常式:
System.Reflection.Assembly thisExe; thisExe = System.Reflection.Assembly.GetExecutingAssembly(); System.IO.Stream file = thisExe.GetManifestResourceStream("AssemblyName.ImageFile.jpg"); this.pictureBox1.Image = Image.FromStream(file);將現有影像檔加入至專案,並在 [方案總管] 中將 [建置動作] 屬性設為 [內嵌資源]。
以組件中的資源名稱取代 "AssemblyName.ImageFile.jpg"。
使用 Assembly 物件的 GetManifestResourceNames 方法來尋找資源名稱。
穩固程式設計
以下情形可能會導致例外狀況:
組件中沒有內嵌資源,而且呼叫 GetManifestResourceStream 會傳回 Nothing。
可能沒有與檔案類型 (即副檔名) 關聯的應用程式。