9

I'm trying to use an icon that I've added as a resource as the image on a button. I know it's possible because I can do it in other projects through the designer. However, I'm trying to do this with code. I added the icon as a resource to my project by following the steps in the accepted answer to this question. The resource is named CancelButtonIcon.

Now, I'm trying to add that icon as the image on a standard button with this code:

this.CancelButton.Image = (System.Drawing.Image)Properties.Resources.CancelButtonIcon;

However, I get an error message:

Cannot convert type 'System.Drawing.Icon' to 'System.Drawing.Image'

In the code that Visual Studio automatically generates when I use the designer, it looks like this:

((System.Drawing.Image)(resources.GetObject("SaveButton.Image")));

which results from manually adding a resource through the Properties window. How can I convert this icon resource to an image so it can be used on the button? Adding it through the designer is not an option (this button is created programmatically and thus isn't present in the designer).

Community
  • 1
  • 1
Ricardo Altamirano
  • 14,650
  • 21
  • 72
  • 105

2 Answers2

17

You can use the Icon.ToBitmap method for this purpose. Note that a Bitmap is an Image.

CancelButton.Image = Properties.Resources.CancelButtonIcon.ToBitmap();
Ani
  • 111,048
  • 26
  • 262
  • 307
  • Perfect and instantaneous. Once the time limit elapses I'll accept this. – Ricardo Altamirano Jun 22 '12 at 19:30
  • Actually implementing this makes me notice something, however. When I add icons to buttons in the designer, they're scaled automatically so they fit the button. This doesn't occur when I use your code, so the icons are *much* larger than the buttons. – Ricardo Altamirano Jun 22 '12 at 19:39
  • 2
    If you want to scale, use `new Bitmap(Properties.Resources.CancelButtonIcon.ToBitmap(), CancelButton.Size)` – Ani Jun 22 '12 at 19:47
  • That scales the image so that it fills the *entire* button horizontally, so most of the vertical part of the image is cut off. When doing this in the designer, it scales the image so that the *entire* image fits on the button, and I use the alignment properties to select where I want it on the button. This solution just fills the entire button with a small part of the image. – Ricardo Altamirano Jun 22 '12 at 19:53
  • I suggest you try it out on the designer with the *actual* icon you are using programmatically and report back. My guess is that you have a greater respect for the designer than is warranted. :) – Ani Jun 22 '12 at 19:56
  • I first created the button in the designer, formatted it, selected its properties, etc. then copied that information into my code and out of the Designer.cs file and into my function. If I use the resource dialogue that comes up when using the properties window on that button, I get the behaviour I describe. When I use your solution in code and using a resource added through the steps I describe, I get the problematic behaviour. – Ricardo Altamirano Jun 22 '12 at 20:01
  • I'm actually going to drop icons from this section of the application all together, so I don't think it will be a concern for me for the time being. – Ricardo Altamirano Jun 22 '12 at 20:03
1

Not sure why, but any time I tried using the accepted answer's approach, the .ToBitmap() call was giving me array index out of bounds exceptions. I solved this by doing it this way instead:

System.Drawing.Icon.FromHandle(Properties.Resources.CancelButtonIcon.Handle).ToBitmap();
HotN
  • 4,216
  • 3
  • 40
  • 51