ToolStripButton:以编程方式分配图像有什么问题

发布于 2024-08-13 08:14:03 字数 1012 浏览 6 评论 0原文

有一个带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心的位置 2024-08-20 08:14:03

在 Visual Studio 中,在解决方案资源管理器中打开“属性”文件夹,然后打开 Resources.resx 文件并添加现有图像文件作为资源。然后,您可以通过 Resource 静态类以编程方式使用它:

Image x = Resources.MyResourceImage;

我建议的代码的完整示例:

using System.Windows.Forms;
using Testapplication.Properties;

namespace Testapplication {
    public class Class1 {
        public Class1() {
            Form MyForm = new Form();
            ToolStrip MyToolStrip = new ToolStrip();
            MyForm.Controls.Add(MyToolStrip);

            ToolStripButton MyButton = new ToolStripButton();
            MyToolStrip.Items.Add(MyButton);

            MyButton.Image = Resources.MyResourceImage;

            MyForm.Show();
        }
    }
}

不要忘记将 using 添加到 YourApps 的 Properties 命名空间。您的 Resources.resx (.cs) 文件驻留在该命名空间中,用于提供强类型对象引用(例如图像)。在您的情况下,将“MyResourceImage”替换为“save”(省略引号)。

附:浏览一下我的 Resources.designer.cs 文件中最重要的部分:

internal static System.Drawing.Bitmap MyResourceImage {
    get {
        object obj = ResourceManager.GetObject("MyResourceImage", resourceCulture);
        return ((System.Drawing.Bitmap)(obj));
    }
}

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:

Image x = Resources.MyResourceImage;

A full example of the code I suggest:

using System.Windows.Forms;
using Testapplication.Properties;

namespace Testapplication {
    public class Class1 {
        public Class1() {
            Form MyForm = new Form();
            ToolStrip MyToolStrip = new ToolStrip();
            MyForm.Controls.Add(MyToolStrip);

            ToolStripButton MyButton = new ToolStripButton();
            MyToolStrip.Items.Add(MyButton);

            MyButton.Image = Resources.MyResourceImage;

            MyForm.Show();
        }
    }
}

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:

internal static System.Drawing.Bitmap MyResourceImage {
    get {
        object obj = ResourceManager.GetObject("MyResourceImage", resourceCulture);
        return ((System.Drawing.Bitmap)(obj));
    }
}
有深☉意 2024-08-20 08:14:03

那么您的意思是从嵌入式资源设置图像?

string res = "MyAssembly.Resources.toolStripButton9.Image";
Stream s = this.GetType().Assembly.GetManifestResourceStream( res );
Icon icon = Icon.FromStream( s );

如果有效,请使用 Webleeuws 答案,比这更容易:P

So you mean setting image from an embedded resource?

string res = "MyAssembly.Resources.toolStripButton9.Image";
Stream s = this.GetType().Assembly.GetManifestResourceStream( res );
Icon icon = Icon.FromStream( s );

Use Webleeuws answer if it works, way easier than this :P

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文