Getting the View DPI
Posted: 25 May 2021, 08:41
In WPF there are a couple common ways of getting the current DPI:
Unity itself has a Screen.dpi, which first of all is not differentiating between x and y, and also seems to be incorrect on some devices as discussed here:
https://forum.unity.com/threads/screen- ... id.414014/
So... what is the best way with Noesis to get the DPI value if I want to do some calculations inside my custom controls with it? (In case I want to support UseLayoutRounding)
- Code: Select all
VisualTreeHelper.GetDpi(Visual visual); // Since .NET Framework 4.6.2 - orCode: Select all
var dpiXProperty = typeof(SystemParameters).GetProperty("DpiX", BindingFlags.NonPublic | BindingFlags.Static); var dpiYProperty = typeof(SystemParameters).GetProperty("Dpi", BindingFlags.NonPublic | BindingFlags.Static); var pixelsPerInchX = (int)dpiXProperty.GetValue(null, null); DpiX = (double)pixelsPerInchX; var pixelsPerInchY = (int)dpiYProperty.GetValue(null, null); DpiY = (double)pixelsPerInchY; - Code: Select all
var source = PresentationSource.FromVisual(visual); var dpiX = One; var dpiY = One; if (source?.CompositionTarget != null) { dpiX = NinetySix * source.CompositionTarget.TransformToDevice.M11; dpiY = NinetySix * source.CompositionTarget.TransformToDevice.M22; }
Unity itself has a Screen.dpi, which first of all is not differentiating between x and y, and also seems to be incorrect on some devices as discussed here:
https://forum.unity.com/threads/screen- ... id.414014/
So... what is the best way with Noesis to get the DPI value if I want to do some calculations inside my custom controls with it? (In case I want to support UseLayoutRounding)