【问题标题】:How can I get the DPI in WPF?如何在 WPF 中获取 DPI?
【发布时间】:2009-12-17 01:11:50
【问题描述】:

如何在 WPF 中获取 DPI?

【问题讨论】:

  • 为什么你需要在 WPF 中做所有事情,一旦你得到它,你将如何处理这个值? WPF 无法在设备相关像素中指定任何坐标。看起来它的大小以像素为单位,但那些是“虚拟像素”——像素的大小,因为它是 96 DPI。如果您更改系统 DPI,它会增大或缩小,因此使用 WPF 绘制的“一像素粗”线在物理上可能不是一像素粗。
  • 因为我想对像素进行四舍五入
  • “SnapToDevicePixels”对您不起作用吗?
  • SnapToDevicePixels 不能很好地工作。事实上,微软为此引入了 UseLayoutRounding。但是 UseLayoutRounding 是“全有或全无”。您不能对某些坐标进行四舍五入,但对其他坐标则不能。
  • @PavelMinaev 一个经常使用的场景是选择适当大小的光标文件以在 WPF 中使用。 WPF 当前不支持多 DPI 光标文件,因此您需要根据屏幕 DPI 加载不同的光标文件。将在 .NET 4.6 中添加支持。

标签: wpf dpi


【解决方案1】:

https://docs.microsoft.com/en-us/archive/blogs/jaimer/getting-system-dpi-in-wpf-app 似乎有效

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

【讨论】:

  • 请记住,虽然 WPF 单位不是像素,但它们与设备无关 @ 96DPI "pixelish-units";所以你真正想要的是 96DPI 和当前 DPI 之间的比例因子(比如 1.5 代表 144DPI)
  • 那么这不是我需要的:(我如何获得比例因子?
  • 我应该使用 GetDeviceCaps (.., LOGPIXELSX) 吗?
  • @tom 这只是 [dpiX, dpiY] / 96.0
  • 您的代码修复了我的 WritableBitmap,因此我想要的网格现在可以正常显示。谢谢:-)
【解决方案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);

【讨论】:

  • 即使您没有对控件的引用,此方法也可以工作,但它确实使用反射,因此它有其优点和缺点,但对于我的情况,这个更好,因为我没有访问权限到一个控件。
  • 此方法的优点是它在窗口的 Loaded 事件之前工作。 PresentationSource.FromVisual(myWindow) 在此之前返回 null。
  • 像魅力一样工作。我喜欢这种方法没有任何假设,这与其他 96 dpi 的版本不同。
  • 这会返回系统 DPI,它是主显示器的 DPI,可能是也可能不是您想要的。如果您希望将 WPF 坐标转换为屏幕坐标,您必须引用一个 Visual,因为从 Windows 8.1 开始,窗口的 DPI 可能会有所不同,具体取决于它所在的监视器,如果这些监视器具有不同的DPI。
【解决方案3】:

使用 .NET 4.6.2 预览版及更高版本,您可以致电 VisualTreeHelper.GetDpi(Visual visual)。它返回一个DpiScale 结构,它告诉你给定Visual 将被或已经被渲染的DPI。

【讨论】:

  • 我无法调用此函数,它显示为未定义。你知道为什么吗?我正在这样做:VisualTreeHelper.GetDpi()
  • @AlexRosenfeld 确保您使用的是命名空间 System.Windows.Media,并且您的目标框架版本至少为 4.6.2。您还需要将 Visual 类型的对象传递给此 api。
【解决方案4】:

我发现获得“真实”显示器 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,
}

【讨论】:

  • [DllImport("Shcore.dll")] - 表示仅适用于 Windows 8 及更高版本
  • 您没有粘贴您的 using 语句。 DpiType 包含在什么类中?
  • 这行得通,但我得到了不同的 X,Y Dpi 大小(可能是因为我在虚拟机上使用带有视网膜的 macbookpro)但是 RawDpi = dpiX; 行我改为 RawDpi = Math.Max(dpiX,dpiY); 所以 RawDpi 是两者中较大的一个
【解决方案5】:

我从 2015 年开始更新我的答案。这是一些使用 Windows 10 中最新 DPI 功能的实用程序代码(特别是 GetDpiForWindow function,这是唯一支持窗口/应用程序/进程等的 DPI_AWARENESS 的方法)但回退到旧版本(每台显示器的 dpi 和桌面 dpi),因此它仍应适用于 Windows 7。

它不依赖于 WPF 和 Winforms,只依赖于 Windows 本身。

// note this class considers dpix = dpiy
public static class DpiUtilities
{
    // you should always use this one and it will fallback if necessary
    // https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getdpiforwindow
    public static int GetDpiForWindow(IntPtr hwnd)
    {
        var h = LoadLibrary("user32.dll");
        var ptr = GetProcAddress(h, "GetDpiForWindow"); // Windows 10 1607
        if (ptr == IntPtr.Zero)
            return GetDpiForNearestMonitor(hwnd);

        return Marshal.GetDelegateForFunctionPointer<GetDpiForWindowFn>(ptr)(hwnd);
    }

    public static int GetDpiForNearestMonitor(IntPtr hwnd) => GetDpiForMonitor(GetNearestMonitorFromWindow(hwnd));
    public static int GetDpiForNearestMonitor(int x, int y) => GetDpiForMonitor(GetNearestMonitorFromPoint(x, y));
    public static int GetDpiForMonitor(IntPtr monitor, MonitorDpiType type = MonitorDpiType.Effective)
    {
        var h = LoadLibrary("shcore.dll");
        var ptr = GetProcAddress(h, "GetDpiForMonitor"); // Windows 8.1
        if (ptr == IntPtr.Zero)
            return GetDpiForDesktop();

        int hr = Marshal.GetDelegateForFunctionPointer<GetDpiForMonitorFn>(ptr)(monitor, type, out int x, out int y);
        if (hr < 0)
            return GetDpiForDesktop();

        return x;
    }

    public static int GetDpiForDesktop()
    {
        int hr = D2D1CreateFactory(D2D1_FACTORY_TYPE.D2D1_FACTORY_TYPE_SINGLE_THREADED, typeof(ID2D1Factory).GUID, IntPtr.Zero, out ID2D1Factory factory);
        if (hr < 0)
            return 96; // we really hit the ground, don't know what to do next!

        factory.GetDesktopDpi(out float x, out float y); // Windows 7
        Marshal.ReleaseComObject(factory);
        return (int)x;
    }

    public static IntPtr GetDesktopMonitor() => GetNearestMonitorFromWindow(GetDesktopWindow());
    public static IntPtr GetShellMonitor() => GetNearestMonitorFromWindow(GetShellWindow());
    public static IntPtr GetNearestMonitorFromWindow(IntPtr hwnd) => MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
    public static IntPtr GetNearestMonitorFromPoint(int x, int y) => MonitorFromPoint(new POINT { x = x, y = y }, MONITOR_DEFAULTTONEAREST);

    private delegate int GetDpiForWindowFn(IntPtr hwnd);
    private delegate int GetDpiForMonitorFn(IntPtr hmonitor, MonitorDpiType dpiType, out int dpiX, out int dpiY);

    private const int MONITOR_DEFAULTTONEAREST = 2;

    [DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr LoadLibrary(string lpLibFileName);

    [DllImport("kernel32", CharSet = CharSet.Ansi, SetLastError = true)]
    private static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName);

    [DllImport("user32")]
    private static extern IntPtr MonitorFromPoint(POINT pt, int flags);

    [DllImport("user32")]
    private static extern IntPtr MonitorFromWindow(IntPtr hwnd, int flags);

    [DllImport("user32")]
    private static extern IntPtr GetDesktopWindow();

    [DllImport("user32")]
    private static extern IntPtr GetShellWindow();

    [StructLayout(LayoutKind.Sequential)]
    private partial struct POINT
    {
        public int x;
        public int y;
    }

    [DllImport("d2d1")]
    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
    }
}

public enum MonitorDpiType
{
    Effective = 0,
    Angular = 1,
    Raw = 2,
}

【讨论】:

    【解决方案6】:

    这就是我设法在 WPF 中获得“比例因子”的方法。 我的笔记本电脑的分辨率是 1920x1440。

    int resHeight = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;  // 1440
    int actualHeight = SystemParameters.PrimaryScreenHeight;  // 960
    double ratio = actualHeight / resHeight;
    double dpi = resHeigh / actualHeight;  // 1.5 which is true because my settings says my scale is 150%
    

    【讨论】:

    • 不幸的是 SystemParameters 仅在主屏幕上提供信息。但如果一个人只对此感兴趣,这是最简单的方法。
    【解决方案7】:

    使用GetDeviceCaps函数:

        static void Main(string[] args)
        {
            // 1.25 = 125%
            var dpi = GetDpi();
        }
    
        [DllImport("user32.dll")]
        public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
    
        [DllImport("user32.dll")]
        public static extern IntPtr GetDC(IntPtr hwnd);
    
        [DllImport("gdi32.dll")]
        static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
    
        private static float GetDpi()
        {
            IntPtr desktopWnd = IntPtr.Zero;
            IntPtr dc = GetDC(desktopWnd);
            var dpi = 100f;
            const int LOGPIXELSX = 88;
            try
            {
                dpi = GetDeviceCaps(dc, LOGPIXELSX);
            }
            finally
            {
                ReleaseDC(desktopWnd, dc);
            }
            return dpi / 96f;
        }
    

    【讨论】:

    • 好答案,但请注意 X 轴和 Y 轴的调用不同。
    【解决方案8】:

    您可以尝试使用 ManagementClass:

    public static string GetDPI()
            {
                using (ManagementClass mc = new ManagementClass("Win32_DesktopMonitor"))
                {
                    using (ManagementObjectCollection moc = mc.GetInstances())
                    {
    
                        int PixelsPerXLogicalInch = 0; // dpi for x
                        int PixelsPerYLogicalInch = 0; // dpi for y
    
                        foreach (ManagementObject each in moc)
                        {
                            PixelsPerXLogicalInch = int.Parse((each.Properties["PixelsPerXLogicalInch"].Value.ToString()));
                            PixelsPerYLogicalInch = int.Parse((each.Properties["PixelsPerYLogicalInch"].Value.ToString()));
                        }
                        return PixelsPerXLogicalInch + "," + PixelsPerYLogicalInch;
                    }
                }
            }
    

    【讨论】:

      【解决方案9】:

      https://blogs.windows.com/buildingapps/2017/01/25/calling-windows-10-apis-desktop-application/#FJtMAIFjbtXiLQAp.97

      2017 年 1 月 25 日下午 3:54

      “从桌面应用程序调用 Windows 10 API” 和

      https://docs.microsoft.com/en-us/uwp/api/windows.devices.display.displaymonitor

      “显示​监视器类”

      命名空间:Windows.Devices.Display 程序集:Windows.Devices.Display.dll、Windows.dll

      提供有关连接到系统的显示监视器设备的信息。

      这些数据包括来自显示器的扩展显示标识数据(EDID,这是一个行业标准的显示描述符块,几乎所有显示器都使用它来提供支持的模式和一般设备信息的描述)和 DisplayID(这是一个提供 EDID 超集的较新的行业标准)。

      原始​DpiX
      获取显示器的物理水平 DPI(基于显示器的原始分辨率和物理尺寸)。

      原始​DpiY
      获取显示器的物理垂直 DPI(基于显示器的原始分辨率和物理尺寸)。

      【讨论】:

        【解决方案10】:

        2006 年 Windows 中的基本监视器信息

        https://docs.microsoft.com/en-us/windows/desktop/wmicoreprov/msmonitorclass

        MSMonitorClass 类

        WmiMonitorRawEEdidV1Block 类

        WmiMonitorBasicDisplayParams 类

        MaxHorizontalImageSize ( EDID byte 21 )
        
        MaxVerticalImageSize ( EDID byte 22 )
        

        (EDID 中的大小以厘米为单位,在 EDID 详细时序描述符中以毫米为单位

        12 水平图像尺寸,毫米,8 lsbits(0–4095 毫米,161 英寸)
        13 垂直图像尺寸,毫米,8 lsbits(0–4095 毫米,161 英寸)
        14 位 7–4 水平图像大小,毫米,4 毫秒
        位 3–0 垂直图像大小,mm,4 msbits

        )

        https://social.msdn.microsoft.com/Forums/vstudio/en-US/e7bb9384-b343-4543-ac0f-c98b88a7196f/wpf-wmi-just-get-an-empty-string

        【讨论】:

          猜你喜欢
          • 2011-06-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多