【问题标题】:Is there a way to retrieve a device's DPI without using GDI+/WinForms "Graphics" class?有没有办法在不使用 GDI+/WinForms“图形”类的情况下检索设备的 DPI?
【发布时间】:2013-06-26 17:07:22
【问题描述】:

是否可以使用本机 win api 调用从 c# 获取我的设备 DPI?

我知道如何从 Windows 窗体应用程序中获取 dpi,我拥有的当前代码是:

  Graphics g = Graphics.FromImage(new Bitmap(10, 10));

  var scaleX = g.DpiX / 96.0f;
  var scaleY = g.DpiY / 96.0f;

我想知道这是否是一个可以让事情变得更简单的 win api 调用。

【问题讨论】:

  • 该代码不会做任何与 Control.CreateGraphics() 不同的事情。 winapi 调用永远不会让任何事情变得更容易。 “我的设备”并不能准确描述您所说的设备类型。
  • @HansPassant 'my devices' 是指运行应用程序的设备。

标签: c# .net windows winapi graphics


【解决方案1】:

a WMI Query to the Win32_DesktopMonitor class 呢?

PixelsPerXLogicalInch

Data type: uint32
Access type: Read-only
Qualifiers: Units (Pixels per Logical Inch) 

显示器沿 x 轴(水平方向)的分辨率。

PixelsPerYLogicalInch

Data type: uint32
Access type: Read-only
Qualifiers: Units (Pixels per Logical Inch) 

显示器沿 y 轴(垂直方向)的分辨率。

你可以用类似to this question:

ManagementObjectSearcher monitorObjectSearch = new ManagementObjectSearcher("SELECT * FROM Win32_DesktopMonitor");

foreach (ManagementObject monitor in monitorObjectSearch.Get())
{
      Debug.WriteLine(monitor["PixelsPerXLogicalInch");
      Debug.WriteLine(monitor["PixelsPerYLogicalInch");
}

还有Windows API Route with GetDeviceCaps,但我听说有some issues with it on Windows 7,所以你的里程可能会有所不同。

还有Direct2D GetDesktopDpi (mentioned by Alex),它看起来需要执行一些 COM 互操作调用,并且可能不那么干净,并且仅适用于 Direct2D 可用的 Windows 版本。 Direct2D 和 .NET 上的一些 additional information

【讨论】:

  • 调用 Grahics 对象 IMO 似乎不太麻烦。但是很好的解决方案。
  • 您确定 PixelsPerXLogicalInch 吗?在 Windows 10 PixelsPerXLogicalInch 上,我设置的每个 DPI 总是返回 96。
【解决方案2】:

有一个 api,http://msdn.microsoft.com/en-us/library/windows/desktop/dd371316(v=vs.85).aspx 称为 getDesktopApi,但我从未使用过它。我认为这就像进行任何互操作调用一样简单。

【讨论】:

    猜你喜欢
    • 2012-07-21
    • 2022-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-18
    • 2016-01-10
    • 2016-01-19
    相关资源
    最近更新 更多