通过GetManifestResourceStream加载文件出现错误提示“null值”对于“stream”无效[转]

时间:2023-03-09 14:30:59
通过GetManifestResourceStream加载文件出现错误提示“null值”对于“stream”无效[转]

本文解决了我的问题,收藏一下。

原文地址:http://blog.sina.com.cn/s/blog_a67799f601010atz.html

在做Mobile开发时,需要引入图片,用到了这个方法:

private Bitmap BackgroundImg = new Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(@"SmartDeviceProject1.bgmain.jpg"));

注:SmartDeviceProject1 :项目名称。bgmain.jpg :图片名称。

编译时候错误提示:“ null值”对于“stream”无效。 低级的错误让我郁闷了一天,最终解决办法如下:

1,把文件bgmain.jpg添加到项目中(在项目上右键—〉添加—〉添加现有项—〉找到并选择bgmain.jpg)

2,在已经添加到项目中的bgmain.jpg上右键—〉属性—〉生成的操作—〉选择“嵌入的资源”

3,正常运行

==============================================================================

现在就可以用这个图片给mobile的Form窗口绘制背景图片了:

private void Form1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawImage(BackgroundImg, 0, 0);

}