解决问题:1)当需要抓取显示器分辨率的缩放比例时。2)当屏幕显示缩放设置不等于100%,导致分辨率改变,Screen.PrimaryScreen.Bounds抓取不到实际设置的分辨率时。
解决方案:使用GetDeviceCaps函数。
/// <summary> /// 设备数据函数 /// </summary> /// <param name="hdc"></param> /// <param name="nIndex"></param> /// <returns></returns> [DllImport("gdi32.dll", EntryPoint = "GetDeviceCaps", SetLastError = true)] public static extern int GetDeviceCaps(IntPtr hdc, int nIndex); private static double GetScreenScalingFactor() { var g = Graphics.FromHwnd(IntPtr.Zero); IntPtr desktop = g.GetHdc(); var physicalScreenHeight = GetDeviceCaps(desktop, 117);//硬件显示器分辨率实际设置的高 var physicalScreenWidth = GetDeviceCaps(desktop, 118);//硬件显示器分辨率实际设置的宽 var screenScalingFactor = (double)physicalScreenHeight / Screen.PrimaryScreen.Bounds.Height;//将实际的高除以抓取的换算过的高就等于缩放率,同理当需要获取实际宽高时,直接获取函数结果即可 return screenScalingFactor; }
标签:GetDeviceCaps,缩放,分辨率,获取,设置,var,屏幕 From: https://www.cnblogs.com/blossomwei/p/17874698.html