wpf dpi

¿Cómo puedo obtener el DPI en WPF?



(5)

Con .NET 4.6.2 Preview y superior, puede llamar a VisualTreeHelper.GetDpi(Visual visual) . Devuelve una estructura DpiScale , que le indica el DPI en el que se representará o se representará el Visual dado.

¿Cómo puedo obtener el DPI en WPF?


Este es un método que se basa en la tecnología Direct2D (compatible con Windows Vista con SP2 y superior y servidores), por lo que funciona bien en WPF (que se basa en los mismos motivos). Utiliza el método ID2D1Factory :: GetDesktopDpi

public static class DpiUtilities { private static Lazy<Tuple<float, float>> _dpi = new Lazy<Tuple<float, float>>(ReadDpi); public static float DesktopDpiX { get { return _dpi.Value.Item1; } } public static float DesktopDpiY { get { return _dpi.Value.Item2; } } public static void Reload() { ID2D1Factory factory; int hr = D2D1CreateFactory(D2D1_FACTORY_TYPE.D2D1_FACTORY_TYPE_SINGLE_THREADED, typeof(ID2D1Factory).GUID, IntPtr.Zero, out factory); if (hr != 0) Marshal.ThrowExceptionForHR(hr); factory.ReloadSystemMetrics(); Marshal.ReleaseComObject(factory); } private static Tuple<float, float> ReadDpi() { ID2D1Factory factory; int hr = D2D1CreateFactory(D2D1_FACTORY_TYPE.D2D1_FACTORY_TYPE_SINGLE_THREADED, typeof(ID2D1Factory).GUID, IntPtr.Zero, out factory); if (hr != 0) Marshal.ThrowExceptionForHR(hr); float x; float y; factory.GetDesktopDpi(out x, out y); Marshal.ReleaseComObject(factory); return new Tuple<float, float>(x, y); } [DllImport("d2d1.dll")] private static extern int D2D1CreateFactory(D2D1_FACTORY_TYPE factoryType, [MarshalAs(UnmanagedType.LPStruct)] Guid riid, IntPtr pFactoryOptions, out ID2D1Factory ppIFactory); private enum D2D1_FACTORY_TYPE { D2D1_FACTORY_TYPE_SINGLE_THREADED = 0, D2D1_FACTORY_TYPE_MULTI_THREADED = 1, } [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] [Guid("06152247-6f50-465a-9245-118bfd3b6007")] private interface ID2D1Factory { int ReloadSystemMetrics(); [PreserveSig] void GetDesktopDpi(out float dpiX, out float dpiY); // the rest is not implemented as we don''t need it } }


La única forma que encontré para obtener el dpi "real" del monitor es la siguiente. Todas las demás técnicas mencionadas solo dicen 96, lo que no es correcto para la mayoría de los monitores.

public class ScreenInformations { public static uint RawDpi { get; private set; } static ScreenInformations() { uint dpiX; uint dpiY; GetDpi(DpiType.RAW, out dpiX, out dpiY); RawDpi = dpiX; } /// <summary> /// Returns the scaling of the given screen. /// </summary> /// <param name="dpiType">The type of dpi that should be given back..</param> /// <param name="dpiX">Gives the horizontal scaling back (in dpi).</param> /// <param name="dpiY">Gives the vertical scaling back (in dpi).</param> private static void GetDpi(DpiType dpiType, out uint dpiX, out uint dpiY) { var point = new System.Drawing.Point(1, 1); var hmonitor = MonitorFromPoint(point, _MONITOR_DEFAULTTONEAREST); switch (GetDpiForMonitor(hmonitor, dpiType, out dpiX, out dpiY).ToInt32()) { case _S_OK: return; case _E_INVALIDARG: throw new ArgumentException("Unknown error. See https://msdn.microsoft.com/en-us/library/windows/desktop/dn280510.aspx for more information."); default: throw new COMException("Unknown error. See https://msdn.microsoft.com/en-us/library/windows/desktop/dn280510.aspx for more information."); } } //https://msdn.microsoft.com/en-us/library/windows/desktop/dd145062.aspx [DllImport("User32.dll")] private static extern IntPtr MonitorFromPoint([In]System.Drawing.Point pt, [In]uint dwFlags); //https://msdn.microsoft.com/en-us/library/windows/desktop/dn280510.aspx [DllImport("Shcore.dll")] private static extern IntPtr GetDpiForMonitor([In]IntPtr hmonitor, [In]DpiType dpiType, [Out]out uint dpiX, [Out]out uint dpiY); const int _S_OK = 0; const int _MONITOR_DEFAULTTONEAREST = 2; const int _E_INVALIDARG = -2147024809; } /// <summary> /// Represents the different types of scaling. /// </summary> /// <seealso cref="https://msdn.microsoft.com/en-us/library/windows/desktop/dn280511.aspx"/> public enum DpiType { EFFECTIVE = 0, ANGULAR = 1, RAW = 2, }



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