C# 获取当前屏幕DPI
2021-04-08 09:25
标签:bsp alt win phi inf tin string nbsp span 1.通过Graphics类获取 比如当前屏幕设置DPI设置1.5倍,可以通过如上通过后台获取。 2.通过Win32-ManagementClass 值得一提的是,有主副屏屏幕的情况下,是获取不了副屏的DPI的! 注:屏幕的物理分辨率/真实分辨率/高宽缩放比例,可参考C# API 获取系统DPI缩放倍数跟分辨率大小 C# 获取当前屏幕DPI 标签:bsp alt win phi inf tin string nbsp span 原文地址:https://www.cnblogs.com/lonelyxmas/p/9091138.htmlGraphics currentGraphics = Graphics.FromHwnd(new WindowInteropHelper(mainWindow).Handle);
double dpixRatio = currentGraphics.DpiX/96;
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()));
}
}
}