ToolStripButton:以编程方式分配图像有什么问题
有一个带有 ToolStrip 的表单。此 ToolStrip 包含一个 ToolStripButton。我想为该按钮指定一个图像:
this.btnSaveFile.Image = Bitmap.FromFile("C:\\Work\\Icons\\png\\save.png");
仅当指定路径上有 save.png 时它才有效。否则,我会收到 FileNotFound 异常。
如果我通过表单设计器创建一个表单,Visual Studio 将创建如下代码:
this.toolStripButton9.Image = ((System.Drawing.Image) (resources.GetObject("toolStripButton9.Image")));
toolStripButton9.Image 这里不是真实姓名。 Visual Studio 获取我的文件 save.png 并将其转换为 toolStripButton9.Image。
但我以编程方式创建了一个表单,没有设计器。我的问题是如何以编程方式将图像分配给 ToolStripBotton?
我尝试将图像添加到项目中,但没有多大帮助。我不知道如何让 Visual Studio 抓取它并将其嵌入到我的可执行文件中,这样我就不需要在指定位置使用此文件。
在MSDN中,我只看到类似 那个:
this.toolStripButton1.Image = Bitmap.FromFile("c:\\NewItem.bmp");
但它并不像我上面所说的那样工作。我知道有一个简单的解决方案,但没有看到它。你能给我一个提示吗?
There is a Form with a ToolStrip. This ToolStrip contains a ToolStripButton. I want to assign an image to this button:
this.btnSaveFile.Image = Bitmap.FromFile("C:\\Work\\Icons\\png\\save.png");
It works only if there is save.png on specified path. Otherwise, I get an FileNotFound Exception.
If I created a Form via Form Designer, Visual Studio would create a code like this:
this.toolStripButton9.Image = ((System.Drawing.Image) (resources.GetObject("toolStripButton9.Image")));
toolStripButton9.Image here is not a real name. Visual Studio takse my file save.png and transform it into toolStripButton9.Image.
But I create a form programmatically, without Designer. And my question is how to assign an image to the ToolStripBotton programmatically?
I tried to add the image to the project, but it didn't help much. I have no idea how to make Visual Studio grab it and embed into my executable so that I wouldn't need this file on specified location.
In MSDN, I only see the solution like that:
this.toolStripButton1.Image = Bitmap.FromFile("c:\\NewItem.bmp");
But it doesnt' work as I told above. I understand there is a simple solution but don't see it. Could you please give me a hint?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Visual Studio 中,在解决方案资源管理器中打开“属性”文件夹,然后打开 Resources.resx 文件并添加现有图像文件作为资源。然后,您可以通过 Resource 静态类以编程方式使用它:
我建议的代码的完整示例:
不要忘记将 using 添加到 YourApps 的 Properties 命名空间。您的 Resources.resx (.cs) 文件驻留在该命名空间中,用于提供强类型对象引用(例如图像)。在您的情况下,将“MyResourceImage”替换为“save”(省略引号)。
附:浏览一下我的 Resources.designer.cs 文件中最重要的部分:
In Visual Studio, Open your "Properties" folder in the solution explorer, then open the Resources.resx file and add a existing image file as resource. You can then use it programmatically via the Resource static class:
A full example of the code I suggest:
Don't forget to add a using to YourApps' Properties namespace. Your Resources.resx (.cs) file resides in that namespace and is used to provide strong-types object references like images. In your case, replace "MyResourceImage" with "save" (omit the quotes).
ps. A glance at the most important part of my Resources.designer.cs file:
那么您的意思是从嵌入式资源设置图像?
如果有效,请使用 Webleeuws 答案,比这更容易:P
So you mean setting image from an embedded resource?
Use Webleeuws answer if it works, way easier than this :P