24

I have a icon which has a few different sizes (16px, 32px, 64px). I am calling ToBitmap() on it, but it is always returning the 32px image. How do I retrieve the 64px one?

Andy Hin
  • 30,345
  • 42
  • 99
  • 142

7 Answers7

36

Does this help?

Icon sizedIcon = new Icon(Resources.ResourceIcon, new Size(64,64));
Stoio
  • 361
  • 1
  • 2
  • 2
25

For anyone else stumbling upon the same problem, I've found a nice little solution.

Image img = new Icon(Properties.Resources.myIcon, width, height).ToBitmap()
Netfangled
  • 2,071
  • 1
  • 18
  • 28
13

This is a fairly painful limitation in the ResourceManager class. Its GetObject() method doesn't provide a way to pass extra arguments that would allow selecting the returned icon by size. A workaround is to add the icon to the project instead. Use Project + Add Existing Item, select your .ico file. Select the added icon and change the Build Action property to "Embedded Resource".

You can now retrieve the desired icon with code like this:

    public static Icon GetIconFromEmbeddedResource(string name, Size size) {
        var asm = System.Reflection.Assembly.GetExecutingAssembly();
        var rnames = asm.GetManifestResourceNames();
        var tofind = "." + name + ".ICO";
        foreach (string rname in rnames) {
            if (rname.EndsWith(tofind, StringComparison.CurrentCultureIgnoreCase)) {
                using (var stream = asm.GetManifestResourceStream(rname)) {
                    return new Icon(stream, size);
                }
            }
        }
        throw new ArgumentException("Icon not found");
    }

Sample usage:

        var icon1 = GetIconFromEmbeddedResource("ARW04LT", new Size(16, 16));
        var icon2 = GetIconFromEmbeddedResource("ARW04LT", new Size(32, 32));

Beware one possible failure mode: this code assumes that the icon was added to the same assembly that contains the method.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • I couldn't get this to work. The string rname did not include the names of the resources inside the file – Kirsten Jan 15 '13 at 04:57
  • This worked for me. The other solutions did not because I needed the icon from an assembly given its path. I loaded the assembly into a reflection-context only. It is important for the icon to be an "Embedded Resource" in the assembly; otherwise, it doesn't work. – redcurry Jun 19 '15 at 12:38
  • Using VS Express 15, I set the `Persistence` property for Icon in my `Resources.resx` to `Embedded in .resx`; is this equivalent to setting the `Build Action` property to `Embedded Resource`? – Thanasi Poulos Feb 06 '16 at 15:46
  • How do you pull the icon with the 32-bit color vs 8-bit? – tofutim Feb 27 '16 at 01:52
3

The following sets the icon size for all the buttons in the toolbar.
It relies on the resource name being stored in the button tag.

public void SetButtons(object toolstrip, int IconWidth, int IconHeight)
{
    var ts = (ToolStrip) toolstrip;
    var size = new System.Drawing.Size();
    size.Height = IconSize;
    size.Width = IconSize;

    foreach (ToolStripButton tsBtn in ts.Items)
    {
        tsBtn.ImageScaling = ToolStripItemImageScaling.None;
        var resourcename = (String) tsBtn.Tag;
        if (resourcename != null)
        {
            var myIcon = (Icon) Properties.Resources.ResourceManager.GetObject(resourcename);
            var newIcon = new Icon(myIcon, IconWidth, IconHeight);
            tsBtn.Image = newIcon.ToBitmap();
        }
    }
}
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
Kirsten
  • 15,730
  • 41
  • 179
  • 318
1
internal static class IconHelper {
    public static Icon GetSize(this Icon icon, int width, int height) {
        return icon.GetSize(new Size(width, height));
    }

    public static Icon GetSize(this Icon icon, Size size) {
        using(var mem = new MemoryStream()) {
            icon.Save(mem);
            mem.Position = 0;
            return new Icon(mem, size);
        }
    }
}
Asherah
  • 18,948
  • 5
  • 53
  • 72
Tobias Boschek
  • 2,158
  • 3
  • 18
  • 22
1

The size is determined when you first create the Icon instance, so you need to specify which size you want to use when you create it, using one of the Icon constructors that take a Size parameter.

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • Hi thanks. This would make sense, but my icon is inside a resource file. How do I modify the constructor? – Andy Hin Oct 26 '10 at 16:58
0

There is no built-in method in the .Net framework that does this.

Instead, you can use this library.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964