Icons sizing not increasing in higher DPI

浪子不回头ぞ 提交于 2019-12-04 21:39:13

Working with "System.Drawing.Icon" icons you should keep in mind to use a bigger size of the icon if you use DPI greater than 100. The property autosize does not help here.

The file of the icon can contain within different sizes so we can detect actual DPI scale factor and considering this factor to load the icon from the filesystem with the proper size.

The code for detecting DPI factor can look like:

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

Now using Icon(string fileName, int width, int height) from System.Drawing.Icon the initialization of the new instance of the icon can look like:

int size = 48;
int dpiSize = (int)(size * DpiHelper.GetDpiFactor());
Icon dpiIcon = new Icon(filename, new Size(dpiSize, dpiSize));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!