C# 在C中调整系统图标的大小#

C# 在C中调整系统图标的大小#,c#,icons,C#,Icons,我想使用系统图标。警告但它太大了,不适合我的需要。我想调整它的大小 我试过: Icon sizedIcon = new Icon(SystemIcons.Warning, new Size(10,10)); 但它不起作用,图标保持不变 有什么建议吗?由于曾经对Windows 98和Windows 2000的相关支持,.NET图标类已经相当残废,它在过去十年中一直处于停滞状态。除此之外,Windows不支持以系统默认图标大小以外的任何大小加载系统图标。通常是32x32。调整它的大小将不可避免地看

我想使用
系统图标。警告
但它太大了,不适合我的需要。我想调整它的大小

我试过:

Icon sizedIcon = new Icon(SystemIcons.Warning, new Size(10,10));
但它不起作用,图标保持不变


有什么建议吗?

由于曾经对Windows 98和Windows 2000的相关支持,.NET图标类已经相当残废,它在过去十年中一直处于停滞状态。除此之外,Windows不支持以系统默认图标大小以外的任何大小加载系统图标。通常是32x32。调整它的大小将不可避免地看起来很糟糕

请记住,在10x10上,任何图标都不会好看。这只是现代显示器上的一个斑点。你可以从一个接近最终尺寸的图标开始,这样做会有一点进步,所需的大小调整越小,看起来合理的几率就越高。如果可以,请选择图标中可能出现的尺寸。比如16x16,32x32,48x48

不管怎样,这是可以解决的。请记住,由于Windows不直接支持此功能,因此这需要相当多的时间。需要的是直接从系统DLL资源中读取图标。并使用更现代的winapi LoadImage()而不是.NET使用的LoadIcon()。在XP和更高版本上工作

向项目中添加新类并粘贴以下代码:

using System;
using System.Drawing;
using System.Reflection;
using System.Runtime.InteropServices;

public class IconEx : IDisposable {
    public enum SystemIcons {
        Application = 100,
        Asterisk = 104,
        Error = 103,
        Exclamation = 101,
        Hand = 103,
        Information = 104,
        Question = 102,
        Shield = 106,
        Warning = 101,
        WinLogo = 100
    }

    public IconEx(string path, Size size) {
        IntPtr hIcon = LoadImage(IntPtr.Zero, path, IMAGE_ICON, size.Width, size.Height, LR_LOADFROMFILE);
        if (hIcon == IntPtr.Zero) throw new System.ComponentModel.Win32Exception();
        attach(hIcon);

    }
    public IconEx(SystemIcons sysicon, Size size) {
        IntPtr hUser = GetModuleHandle("user32");
        IntPtr hIcon = LoadImage(hUser, (IntPtr)sysicon, IMAGE_ICON, size.Width, size.Height, 0);
        if (hIcon == IntPtr.Zero) throw new System.ComponentModel.Win32Exception();
        attach(hIcon);
    }


    public Icon Icon {
        get { return this.icon; }
    }

    public void Dispose() {
        if (icon != null) icon.Dispose();
    }

    private Icon icon;

    private void attach(IntPtr hIcon) {
        // Invoke the private constructor so we can get the Icon object to own the handle
        var ci = typeof(Icon).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, 
            null, new Type[] { typeof(IntPtr), typeof(bool) }, null);
        this.icon = (Icon)ci.Invoke(new object[] { hIcon, true});
    }

    private const int IMAGE_ICON = 1;
    private const int LR_LOADFROMFILE = 0x10;
    private const int LR_SHARED = 0x8000;

    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    private static extern IntPtr GetModuleHandle(string name);
    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    private static extern IntPtr LoadImage(IntPtr hinst, string lpszName, int uType,
                                 int cxDesired, int cyDesired, int fuLoad);
    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    private static extern IntPtr LoadImage(IntPtr hinst, IntPtr resId, int uType,
                                 int cxDesired, int cyDesired, int fuLoad);
}
示例用法:

    using (var icon = new IconEx(IconEx.SystemIcons.Warning, new Size(10, 10))) {
        e.Graphics.DrawIcon(icon.Icon, 0, 0);
    }

它看起来确实比系统图标要好。警告。当你使用16x16时,它非常干净。

你需要它作为一个图标还是一个abitmap?@TaW我需要它作为一个图标。我不知道我为什么要为你的答案投票。你总是在早上的第一件事就是>200。对阿根廷队的比赛感到抱歉。感谢,这是一场悲伤的比赛。还没有完全结束,巴西周六。