开发者

Selecting the size of a System.Drawing.Icon?

I have a ic开发者_Python百科on 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?


Does this help?

Icon sizedIcon = new Icon(Resources.ResourceIcon, new Size(64,64));


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()


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.


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


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.


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


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

Instead, you can use this library.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜