Icons sizing not increasing in higher DPI

自作多情 提交于 2020-01-13 07:24:11

问题


I am facing this issue while DPI is greater than 100. I have changed the size of the icons such as bold, italics and when I run the program in 100 dpi, the icons sizes are bigger but when i run the program after changing to dpi greater than 100 the icons are getting smaller, and not updating to any size values . I have tried autosize = false, imagescaling to none.


回答1:


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


来源:https://stackoverflow.com/questions/52288972/icons-sizing-not-increasing-in-higher-dpi

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