ToolStripButton: what's wrong with assigning an image programmatically

给你一囗甜甜゛ 提交于 2019-12-05 17:40:18

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));
    }
}

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!