Selecting the size of a System.Drawing.Icon?

空扰寡人 提交于 2019-12-28 01:04:12

问题


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?


回答1:


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.




回答2:


Does this help?

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



回答3:


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



回答4:


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



回答5:


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



回答6:


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

Instead, you can use this library.




回答7:


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.



来源:https://stackoverflow.com/questions/4025401/selecting-the-size-of-a-system-drawing-icon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!