简体   繁体   中英

DPI Percentage Scaling to actual DPI value

As we know the Windows 7 available DPI scalings are 100%,125%,150% and 200%. The actual DPI value for these four DPI percentages are

Percentage - DPI Values
100%       - 96
125%       - 120
150%       - 144
200%       - 192

refer the link for DPI scaling: http://www.techrepublic.com/blog/windows-and-office/get-a-better-view-in-windows-7-by-adjusting-dpi-scaling/

Using C# i want to take the DPI value. So by following C# code am trying to achieve.

float x=0;
float y=0;
Graphics gp = Graphics.FromHwnd(IntPtr.Zero);// we can also use this.CreateGraphics()
x = gp.DpiX;
y = gp.DpiY;

Am getting the output as follows, which is wrong for 150% and 200%

100%  -  96 //both x,y values
125%  - 120 //both x,y values
**150%  -  96 //both x,y values
200%  -  96 //both x,y values**

If you haven't declared your application as "DPI aware", Windows will lie to you, pretend that it is set to 96 DPI (although it isn't) and take care of scaling your application itself.

You can "fix" this by either

  • adding a dpiAware -entry to your application manifest file or
  • calling the SetProcessDPIAware Windows API method.

Examples for both can be found, for example, in this SO answer:

To make things even more complicated, Windows 8.1 introduced per-monitor DPI settings, deprecating SetProcessDPIAware . This is actually a good thing, but it's hard to get it right .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM