C# winform 获取屏幕分辨率缩放率,获取屏幕设置分辨率
解决问题: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; }