4

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**
TaW
  • 53,122
  • 8
  • 69
  • 111
Shazif
  • 43
  • 5

1 Answers1

1

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.

Community
  • 1
  • 1
Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • But the problem is it won't work with Windows XP. Can you pls explain how to resolve this prob in XP. FYI: I went through the below MSDN link. but its not clear. https://msdn.microsoft.com/en-us/library/dn469266(v=vs.85).aspx – Shazif Jan 24 '17 at 05:14