How can I get the DPI in WPF?

tom greene picture tom greene · Dec 17, 2009 · Viewed 48.8k times · Source

How can I get the DPI in WPF?

Answer

Ana Betts picture Ana Betts · Dec 17, 2009

https://docs.microsoft.com/en-us/archive/blogs/jaimer/getting-system-dpi-in-wpf-app 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;
}