KeldorKatarn
Topic Author
Posts: 193
Joined: 30 May 2014, 10:26

Getting the View DPI

25 May 2021, 08:41

In WPF there are a couple common ways of getting the current DPI:
  • VisualTreeHelper.GetDpi(Visual visual); // Since .NET Framework 4.6.2
  • 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;
    or
  • 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;
    }
The first and last of these are not implemented right now by Noesis and the second one is a bit messy (I'm not 100% sure that's even working in Unity's Mono)

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)
 
User avatar
jsantos
Site Admin
Posts: 3918
Joined: 20 Jan 2012, 17:18
Contact:

Re: Getting the View DPI

25 May 2021, 12:23

Support for DPI in the View was implemented for Noesis 3.1. In Unity, right now, we are using Screen.dpi because that's probably going to be the most robust way, even if it is buggy on a few devices.

And we should implement VisualTreeHelper.GetDpi, I have created a ticket for it (#2034)
 
User avatar
jsantos
Site Admin
Posts: 3918
Joined: 20 Jan 2012, 17:18
Contact:

Re: Getting the View DPI

25 May 2021, 12:26

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)
If this helps, this is implemented as a root transform in the view. In NoesisGUI 3.0 this must be done manually as shown in our Controls Gallery.
 
KeldorKatarn
Topic Author
Posts: 193
Joined: 30 May 2014, 10:26

Re: Getting the View DPI

25 May 2021, 13:26

A root transform? I'm not sure how that works.

The usecase I was thinking of was for example what WPF does in the Border class. When LayoutRounding is active, the border thickness is rounded to the nearest integer of device pixels.
I need the DPI to make that calculation inside a custom border class (like is present in my Conductors sample branch in the DecorativeBorder)

Who is online

Users browsing this forum: Bing [Bot] and 21 guests