我怎样才能在WPF中获得DPI?

我怎样才能在WPF中获得DPI?

http://blogs.msdn.com/jaimer/archive/2007/03/07/getting-system-dpi-in-wpf-app.aspx似乎工作;

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

使用.NET 4.6.2 Preview或更高版本,可以调用VisualTreeHelper.GetDpi(Visual visual) 。 它返回一个DpiScale结构,它告诉你给定的Visual将被渲染的DPI。

我发现获得“真正的”监控dpi的唯一方法是以下几点。 所有其他提到的技术只是说96这是不正确的大多数显示器。

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

这是一种依赖于Direct2D技术的方法(在支持SP2及更高版本的Windows Vista和服务器上支持),所以它在WPF(基于相同的理由)下工作正常。 它使用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 } }