tags:

views:

142

answers:

1

How can I get the DPI in WPF?

+6  A: 

http://blogs.msdn.com/jaimer/archive/2007/03/07/getting-system-dpi-in-wpf-app.aspx seems to work

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;
}
Paul Betts
This is exatly what I needed, thanks!
tom greene
Keep in mind though that WPF units aren't pixels, they're device-independent @ 96DPI "pixelish-units"; so really what you want, is the scale factor between 96DPI and the current DPI (so like 1.5 for 144DPI)
Paul Betts
So that's not what I need then :( How do I get the scale factor?
tom greene
Should I use GetDeviceCaps (.., LOGPIXELSX)?
tom greene
@tom it's just [dpiX, dpiY] / 96.0
Paul Betts