views:

308

answers:

3

I would like to create a single System.Drawing.Icon programmatically from 32x32, 16x16 Bitmaps. Is this possible? If I load an icon with -

Icon myIcon = new Icon(@"C:\myIcon.ico");

...it can contain multiple images.

A: 

There's a nice code snippet here. It uses the Icon.FromHandle method.

keyboardP
I think he wants to create an icon with multiple images...
Zach Johnson
+2  A: 

An .ico file can have multiple images in it, but when you load an .ico file and create an Icon object, only one of those images is loaded. Windows chooses the most appropriate image based on the current display mode and system settings and uses that one to initialize the System.Drawing.Icon object, ignoring the others.

So you can't create a System.Drawing.Icon with multiple images, you have to pick one before you create the Icon object.

Of course, It is possible to create an Icon at runtime from a bitmap, is that what you are really asking? Or are you asking how to create an .ico file?

John Knoeller
But if I load an .ico file like I've shown above, I can access the 16x16, 32x32, 48x48 etc. like this -:Icon smallIcon = new Icon(myIcon, 16, 16);
Damien
Because it knows which file/resource the Icon was created from and can go back to it and get a new image. There is no way to ADD an image to an Icon object though.
John Knoeller
A: 

You could try using png2ico to create a .ico file, invoking it using System.Diagnostics.Process.Start. You would need to create your images and save them to disc before invoking png2ico.

Zach Johnson