选择 System.Drawing.Icon 的大小?

发布于 2024-09-29 08:01:08 字数 97 浏览 4 评论 0原文

我有一个有几种不同尺寸(16px、32px、64px)的图标。我在其上调用 ToBitmap(),但它始终返回 32px 图像。我如何检索 64px 的?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

ぃ弥猫深巷。 2024-10-06 08:01:08

这有帮助吗?

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

Does this help?

Icon sizedIcon = new Icon(Resources.ResourceIcon, new Size(64,64));
羁绊已千年 2024-10-06 08:01:08

对于其他遇到同样问题的人,我找到了一个很好的小解决方案。

Image img = new Icon(Properties.Resources.myIcon, width, height).ToBitmap()

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()
婴鹅 2024-10-06 08:01:08

这是 ResourceManager 类中一个相当痛苦的限制。它的 GetObject() 方法不提供传递额外参数的方法,从而允许按大小选择返回的图标。解决方法是将图标添加到项目中。使用“项目”+“添加现有项目”,选择您的 .ico 文件。选择添加的图标并将“构建操作”属性更改为“嵌入资源”。

现在,您可以使用如下代码检索所需的图标:

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

示例用法:

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

注意一种可能的失败模式:此代码假定图标已添加到包含该方法的同一程序集中。

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.

缱倦旧时光 2024-10-06 08:01:08

下面设置工具栏中所有按钮的图标大小。
它依赖于存储在按钮标签中的资源名称。

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 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();
        }
    }
}
樱桃奶球 2024-10-06 08:01:08

大小是在您第一次创建 Icon 实例时确定的,因此您需要在创建它时指定要使用的大小,使用 Icon 构造函数,采用 Size范围。

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.

我们的影子 2024-10-06 08:01:08
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);
        }
    }
}
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);
        }
    }
}
£烟消云散 2024-10-06 08:01:08

.Net 框架中没有内置方法可以执行此操作。

相反,您可以使用此库

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

Instead, you can use this library.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文