8

We can get resolution and stuff for our screen is Screen class. So, we should use Screen.WorkingArea.Width and Screen.WorkingArea.Height, if we want to place something in center of screen.

But, in Windows 8.1 (not sure about other OSs) there is possibility to scale items on screen, if they seem too small. You can check it, by calling context menu on desktop, select "Screen Resolution", and then select "Make text and other items larger or smaller".

And the problem is, that, it seems, it does actually increase screen resolution! So, Screen.WorkingArea.Width and Screen.WorkingArea.Height will give you scaled value, and point Screen.WorkingArea.Width/2 Screen.WorkingArea.Height/2 will no longer in center of actual screen (for 200% scaling this point will be on lower right corner of screen). That might screw a lot of placement of UI items.

So, is it possible to get value of scaling? I can't find any class containing this info.

lentinant
  • 753
  • 1
  • 10
  • 33
  • [Does this help you?](https://msdn.microsoft.com/en-US/library/windows/apps/windows.graphics.display.displayproperties.resolutionscale.aspx) – kevintjuh93 Sep 16 '15 at 11:36
  • It seems like right thing, but now i need to figure out, how to use it with my code. I've found [this arcticle](http://www.codeproject.com/Articles/1285/Calling-API-functions-using-C) about using API methods, but I don't know where exactly this property is. – lentinant Sep 16 '15 at 11:59
  • What kind of application is it? – kevintjuh93 Sep 16 '15 at 12:00
  • WPF application. Is there any specific reference to use Windows API? – lentinant Sep 16 '15 at 12:17
  • 1
    http://stackoverflow.com/questions/2236173/screen-resolution-problem-in-wpf What about this? – kevintjuh93 Sep 16 '15 at 12:21
  • It is just what i need (it also specifies the problem), but solution they are using involves using existing control (which is already rendered), and i need to calculate it before splash screen is shown (because it is actually first item shown). I need to get DPI value somehow before any UI actually shows up. – lentinant Sep 16 '15 at 12:54
  • 1
    Well, it seems, I can always get DPI of screen from registry, Path is "HKEY_CURRENT_USER\Control Panel\Desktop", key-value is "LogPixels". – lentinant Sep 16 '15 at 13:10

3 Answers3

8

Most methods to retrieve DPI depends on existing controls, which may be inconvenient sometimes. But DPI can be always retrieved from registry. In C#:

using Microsoft.Win32;
//...
var currentDPI = (int)Registry.GetValue("HKEY_CURRENT_USER\\Control Panel\\Desktop", "LogPixels", 96);

Scale will be

var scale = 96/(float)currentDPI;
lentinant
  • 753
  • 1
  • 10
  • 33
  • 1
    This registry setting doesn't exist for my Windows 10 machine. (It does exist for my Windows 7 machine.) – RenniePet Nov 24 '18 at 14:43
  • @RenniePet: You probably found it by now but here it is just in case: ```int DPI = Int32.Parse((string)Registry.GetValue( @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\ThemeManager", "LastLoadedDPI", "96"));``` – miran80 Feb 04 '20 at 21:16
0
 var currentDPI = (int)Microsoft.Win32.Registry.GetValue("HKEY_CURRENT_USER\\Control Panel\\Desktop\\WindowMetrics", "AppliedDPI", 0);
 SetClientSizeCore((int)((float)Width * (float)currentDPI/96.0f), (int)((float)Height * (float)currentDPI/96.0f));
  • 3
    Answers are more useful when they include an explanation -- especially as they may be found by a user working in a different language. Please edit the answer and provide some context for this code. – claytond Jan 20 '20 at 21:17
0

Here is a working method of getting DPI information on Windows 10:

int DPI = Int32.Parse((string)Registry.GetValue(
   @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\ThemeManager",
   "LastLoadedDPI", "96"));
miran80
  • 745
  • 4
  • 16