现在的位置: 首页 > 综合 > 正文

C#中使用嵌入资源的图像

2013年12月02日 ⁄ 综合 ⁄ 共 725字 ⁄ 字号 评论关闭
 

System.Reflection.Assembly thisExe;
thisExe 
= System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream file 
= 
    thisExe.GetManifestResourceStream(
"TestPrintProcess.nopic.png");

string [] resources = thisExe.GetManifestResourceNames();
string list = "";

// Build the string of resources.查找程序集中的资源名称
foreach (string resource in resources)
    list 
+= resource + " ";
MessageBox.Show(list);

this.pictureBox1.Image = Image.FromStream(file);

示例需要:
  一个含有名为 pictureBox1 的 PictureBox 控件的 Windows 窗体。

在项目中添加图像文件,然后在解决方案资源管理器中将“生成操作”属性设置为“嵌入的资源”。

"TestPrintProcess.nopic.png" 替换成程序集中已知的资源名称。可以使用程序集对象的 GetManifestResourceNames 方法查找该资源的名称,请注意一定要用程序集名称。

以下情况可能会导致异常:
  嵌入的资源不在程序集中,因此 GetManifestResourceStream 调用返回 Nothing
  该文件类型(文件扩展名)可能没有关联的应用程序。

抱歉!评论已关闭.