如何在WPF中获取DPI?

78

我如何在WPF中获取DPI?


1
你为什么要在WPF中这样做,而一旦你获得值后你将会怎样处理它?WPF没有指定任何基于设备的像素坐标的方法。它的大小似乎为像素,但那些是“虚拟像素” - 像96 DPI一样的像素大小。如果您更改系统DPI,则它会增长或缩小,因此使用WPF绘制的“一个像素粗”的线可能不会物理上是一个像素粗。 - Pavel Minaev
1
因为我想要将像素四舍五入。 - tom greene
1
如果您想要在物理像素边界上进行像素精确定位,最好一开始就不要使用WPF。这不是它设计的情景,并且关于WPF坐标如何四舍五入等方面绝对没有任何保证。如果您只想让顶点捕捉到最近的物理像素,请使用UseLayoutRounding 属性。如果您想要绘制一条恰好有10个物理像素长的线,则忘记使用WPF吧。 - Pavel Minaev
2
“SnapToDevicePixels” 对你无效吗? - Ana Betts
2
SnapToDevicePixels 的效果并不是很好。事实上,微软为此引入了 UseLayoutRounding。 但是 UseLayoutRounding 是“全有或全无”的。您不能仅对某些坐标进行舍入而不对其他坐标进行舍入。 - tom greene
2
@PavelMinaev 一个经常使用的场景是选择适当大小的光标文件在WPF中使用。WPF目前不支持多DPI光标文件,因此您需要根据屏幕DPI加载不同的光标文件。该支持将在.NET 4.6中添加。 - SepehrM
11个回答

83

4
请记住,WPF单位并非像素,它们是基于设备独立的“类像素单位” @ 96DPI;因此,您真正需要的是当前DPI与96DPI之间的比例系数(例如144DPI的比例系数为1.5)。 - Ana Betts
那么那不是我需要的 :( 我怎么得到比例因子? - tom greene
我应该使用 GetDeviceCaps(.., LOGPIXELSX) 吗? - tom greene
2
@tom,这只是[dpiX,dpiY] / 96.0。 - Ana Betts
2
你的代码修复了我的WritableBitmap,所以我想要的网格现在正常显示。谢谢 :-) - Endrju
似乎无法工作。M11和M22始终为1。 - Kux

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

2
这种方法即使在没有控件引用的情况下也可以工作,但它确实使用了反射,因此它有其优缺点,但对于我的情况来说,这个方法更好,因为我无法访问控件。 - Paul Stark
3
这种方法的优点是它可以在窗口的Loaded事件之前就开始运行。在那之前,PresentationSource.FromVisual(myWindow)会返回null。 - Brian Rak
运作得非常好。我喜欢这种方法没有假设,不像其他 96 DPI 版本。 - B.K.
2
这将返回系统 DPI,即主显示器的 DPI,可能不是您想要的。如果您想将 WPF 坐标转换为屏幕坐标,则必须引用 Visual,因为自 Windows 8.1 以来,窗口的 DPI 可能因其所在的监视器具有不同的 DPI 而异。 - caesay

48

我无法调用这个函数,它显示为未定义。你知道为什么吗?我正在执行以下操作:VisualTreeHelper.GetDpi() - xandermonkey
3
@AlexRosenfeld请确保您正在使用命名空间System.Windows.Media,并且您的目标框架版本至少为4.6.2。您还需要将Visual类型的对象传递给此API。 - rohit21agrawal

8
我已更新了我的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://learn.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,
}

7

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

3
[DllImport("Shcore.dll")] - 表示该功能仅适用于Windows 8及以上版本。 - EpiGen
你没有粘贴你的using语句。DpiType类包含在哪个类中? - rollsch
这个可以工作,但我得到了不同的X、Y Dpi大小(可能是因为我在虚拟机上使用MacBook Pro Retina),但是我将行RawDpi = dpiX;更改为RawDpi = Math.Max(dpiX,dpiY);,所以RawDpi是两者中较大的一个。 - tkefauver

4

这是我在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%

2
不幸的是,SystemParameters 只提供主屏幕的信息。但如果只对主屏幕感兴趣,这是我所知道的最简单的方法。 - Blightbuster

2
使用 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;
    }

1
很好的回答,但请注意X轴和Y轴有不同的调用。 - astef

1

您可以尝试使用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;
                }
            }
        }

1

0

这里有

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://learn.microsoft.com/en-us/uwp/api/windows.devices.display.displaymonitor

"显示器类"

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

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

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

RawDpiX
获取显示器的物理水平 DPI(基于显示器的本机分辨率和物理尺寸)。

RawDpiY
获取显示器的物理垂直 DPI(基于显示器的本机分辨率和物理尺寸)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接