通过资源加载应用程序图标
我所做的如下:
我使用 IDE (SharpDevelop) 创建了一个 resource.resx
文件,并向其中添加了一个 logo.ico
。 Resource.resx 文件看起来像这样:
<data name="logo" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAA... (etc etc)
然后我使用 ResGen.exe
将此 resource.resx
编译为有效的 resource.resources
文件。
resgen.exe resource.resx resource.resources
我使用命令行 JScript.NET 编译器编译了所有内容,并包含了包含徽标文件的 resource.resources
。
jsc /res:resource.resources /out:app.exe /reference:UI.dll,Remote.dll app.cs
现在我想通过我的代码访问此徽标文件,因此我得到了以下内容:
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
System.Drawing.Icon icon = new System.Drawing.Icon(assembly.GetManifestResourceStream("resource.resources.logo"));
但是,我收到 GetManifestResourceStream
的运行时异常,该异常为空。
我想这是一个命名空间问题,所以我尝试仅使用 GetManifestResourceStream("resource.resources")
访问资源文件,但我得到了另一个异常:
Argument 'picture' 必须是一张图片可以用作图标。
任何想法将不胜感激,谢谢!
Here's what I've done:
I created a resource.resx
file using my IDE (SharpDevelop) and added an logo.ico
to it. The resource.resx file looks something like this:
<data name="logo" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAA... (etc etc)
Then I used ResGen.exe
to compile this resource.resx
into a valid resource.resources
file.
resgen.exe resource.resx resource.resources
Using the command line JScript.NET compiler I compiled everything and included my resource.resources
which has my logo file.
jsc /res:resource.resources /out:app.exe /reference:UI.dll,Remote.dll app.cs
Now I want to access this logo file from withing my code, so I've got this:
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
System.Drawing.Icon icon = new System.Drawing.Icon(assembly.GetManifestResourceStream("resource.resources.logo"));
However I get a runtime Exception for GetManifestResourceStream
which is null.
I suppose this is a namespace issue so I tried accessing the resource file only with GetManifestResourceStream("resource.resources")
but I get this other Exception:
Argument 'picture' must be a picture that can be used as a Icon.
Any ideas would be greatly appreciated, thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它抱怨文件格式,它无法将其识别为图标。我无法从您的代码片段中看出,没有足够的 Base64 编码值可见。
我不得不说,如果这个 IDE 让您编写这样的代码,那么它对您没有帮助。在 Visual Studio 中,在资源中存储图标是非常简单的,根本不需要编写任何代码。 Express 版本的价格相同。
It is complaining about the file format, it doesn't recognize it as an icon. I can't tell from your snippet, not enough of the base64 encoded value is visible.
I have to say, if this IDE is making you write code like this then it is not helping you. Storing icons in the resources is utterly trivial in Visual Studio, there is no need to write any code at all. The Express edition has the same price.