首页 > 解决方案 > 图标大小在更高 DPI 中没有增加

问题描述

我在 DPI 大于 100 时遇到了这个问题。我更改了图标的大小,例如粗体、斜体,当我以 100 dpi 运行程序时,图标大小更大,但是当我在更改为 dpi 后运行程序时大于 100 图标越来越小,并且没有更新到任何大小值。我试过 autosize = false,图像缩放到无。

标签: highdpidpi-aware

解决方案


使用“System.Drawing.Icon”图标时,如果您使用大于 100 的 DPI,您应该记住使用更大尺寸的图标。属性autosize在这里没有帮助。

图标的文件可以包含不同的大小,因此我们可以检测实际的 DPI 比例因子并考虑这个因子以从文件系统加载具有适当大小的图标。

检测 DPI 因子的代码如下所示:

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

    public static class DpiHelper
    {
        private static readonly double m_dpiKoef = Graphics.FromHdc(GetDC(IntPtr.Zero)).DpiX / 96f;

        public static double GetDpiFactor()
        {
            return m_dpiKoef;
        }

        [DllImport("User32.dll")]
        private static extern IntPtr GetDC(IntPtr hWnd);
    }

现在使用System.Drawing.Icon 中的Icon(string fileName, int width, int height)图标的新实例的初始化可能如下所示:

int size = 48;
int dpiSize = (int)(size * DpiHelper.GetDpiFactor());
Icon dpiIcon = new Icon(filename, new Size(dpiSize, dpiSize));

推荐阅读