错误:“空"的值对“流"无效 [英] error : Value of 'null' is not valid for 'stream'

查看:29
本文介绍了错误:“空"的值对“流"无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打开了一个 WinForms 应用程序项目Resources.resx 文件并在那里复制了图像.我正在使用下面显示的代码从资源中获取图像,但出现以下错误:

I have opened Resources.resx file of a WinForms application project and copied images there. I am using the code shown below to get an image from the resources, but getting following error:

'null' 的值对 'stream' 无效.

Value of 'null' is not valid for 'stream'.

错误出现在这一行:

btn.BackgroundImage = new Bitmap(
  System.Reflection.Assembly.GetEntryAssembly().GetManifestResourceStream(test));

相关代码:

private void genericButton_event(object sender, EventArgs e)
{
    var btn = (Button)sender;
    string test = "StudentModule.Properties.Resources" + btn.Name + ".png";

    //Getting the error here:
    btn.BackgroundImage = new Bitmap(System
                                       .Reflection
                                       .Assembly
                                       .GetEntryAssembly()
                                       .GetManifestResourceStream(test));
}

test的值是"StudentModule.Properties.ResourcesbtnAbout.png",但我觉得应该是:"StudentModule.Properties.Resources.btnAbout.png".我也试过这条线,但它不起作用:

The value of test is "StudentModule.Properties.ResourcesbtnAbout.png", but I think it should be: "StudentModule.Properties.Resources.btnAbout.png". I tried this line also but it's not working:

string test = "StudentModule.Properties.Resources." + btn.Name + ".png";

我在这里做错了什么?

推荐答案

GetManifestResourceStream() 在找不到流时返回 null.然后会轰炸 Bitmap 构造函数.

GetManifestResourceStream() returns null when the stream could not be found. Which will then bomb the Bitmap constructor.

所以你使用的字符串是错误的.目前尚不清楚您是如何嵌入资源的.强烈建议使用资源设计器,项目 + 资源选项卡.单击添加资源"按钮上的箭头,选择添加现有文件"并选择文件.然后你可以使用 ResourceManager 来获取位图:

So the string you used is wrong. It isn't clear exactly how you embedded the resource. Do strongly favor using the resource designer, Project + Resources tab. Click the arrow on the Add Resource button, choose "Add Existing File" and select the file. You can then use the ResourceManager to get the bitmap:

var imageName = "Chrysanthemum";
btn.BackgroundImage = (Bitmap)Properties.Resources.ResourceManager.GetObject(imageName);

注意资源名称是如何在资源设计器中显示的只是简单的资源名称.

Note how the resource name is just the plain resource name as it appears in the resource designer.

这篇关于错误:“空"的值对“流"无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆