gmetax
gmetax

Reputation: 3973

WPF DPI value Per Monitor

Is it possible to get the DPI value of specific monitor?

I have tried all the methods that are online but they are always return the value of the primary monitor.

examples that i have used :

example1

using (Graphics g = Graphics.FromHwnd(IntPtr.Zero))
{
       dpiX = g.DpiX;
       dpiY = g.DpiY;
}

example2

PresentationSource source = PresentationSource.FromVisual(this);

double dpiX, dpiY;
if (source != null) {
    dpiX = 96.0 * source.CompositionTarget.TransformToDevice.M11;
    dpiY = 96.0 * source.CompositionTarget.TransformToDevice.M22;
}

example3

var dpiXProperty = typeof(SystemParameters).GetProperty("DpiX", BindingFlags.NonPublic | BindingFlags.Static);
var dpiYProperty = typeof(SystemParameters).GetProperty("Dpi", BindingFlags.NonPublic | BindingFlags.Static);

var dpiX = (int)dpiXProperty.GetValue(null, null);
var dpiY = (int)dpiYProperty.GetValue(null, null);

none of the above works for specific monitor

Upvotes: 0

Views: 3102

Answers (2)

Colin Smith
Colin Smith

Reputation: 12550

You can use:

GetDpiForMonitor

Now, it depends whether you have said if your application is DPI aware or not, on how you can get to those per monitor dpi values - if you aren't DPI aware, then you could be lied to on the actual DPI.

If you do:

SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE).

(or set it in your app.manfest).

Then what you can do next is enumerate the monitors, and call GetDpiForMonitor to get the actual DPI set by the user for each one.

You could use this code that uses System.Windows.Forms.Screen and MonitorFromPoint to get a handle to each screen.

If you look at:

there's a sample WPF application there, with some helper calls (in managed c++)...but you can equally do your own DLLImports of the WIN32 APIs, etc.

Upvotes: 2

Wouter
Wouter

Reputation: 2958

You need .Net 4.7

See: https://msdn.microsoft.com/en-us/library/ms171868(v=vs.110).aspx

Sections:

  • High DPI support
  • Per-monitor DPI

Upvotes: 0

Related Questions