How can I get the DPI in WPF? How can I get the DPI in WPF? wpf wpf

How can I get the DPI in WPF?


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;}


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);


With .NET 4.6.2 Preview and higher, you can call VisualTreeHelper.GetDpi(Visual visual). It returns a DpiScale structure, which tells you the DPI at which the given Visual will be or has been rendered.