4

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?

Racoon
  • 951
  • 3
  • 14
  • 32

2 Answers2

5

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));
    }
}
Webleeuw
  • 7,222
  • 33
  • 34
  • Webleeuw, thanks for the answer. Sorry but it seems doesn't work. I added save png.file via Properties->Resources. Now I see it in Resource folder under my project. But how can I refer to it? What can I place instead of MyResourceImage"? File resource.Designer.cs was automatically created with this piece of code: object obj = ResourceManager.GetObject("save", resourceCulture); But I failed to use constructions like this... Any idea? – Racoon Nov 26 '09 at 10:37
  • if you let intellisense fill in the blanks. what happens if you type "Resources." what suggestions does it give? – RvdK Nov 26 '09 at 11:25
  • Webleeuw, yes, it works! Thank you very much for this example! – Racoon Nov 26 '09 at 12:00
0

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

RvdK
  • 19,580
  • 4
  • 64
  • 107
  • Thanks :), It works for me, but I don't know if it's the solution Racoon seeks. – Webleeuw Nov 26 '09 at 08:53
  • PoweRoy, thanks for the answer. Sorry, I don't understand. I don't have toolStripButton9.Image since I don't use Form Designer. So I don't have an idea how to use this piece of code... Webleeuws seems close to the solution but ... please see my comment to his answer. – Racoon Nov 26 '09 at 10:43