🐛 修复缩放屏幕截图bug

This commit is contained in:
BookerLiu
2022-07-27 17:13:45 +08:00
parent a682f27223
commit 43314a3006
3 changed files with 40 additions and 23 deletions

View File

@@ -39,7 +39,11 @@ namespace GeekDesk.Control.Windows
{ {
InitializeComponent(); InitializeComponent();
this.colorPicker = colorPicker; this.colorPicker = colorPicker;
SetProcessDPIAware(); try
{
SetProcessDPIAware();
}
catch (Exception e) { }
ColorPickerWindow_Init(); ColorPickerWindow_Init();
} }
@@ -58,9 +62,12 @@ namespace GeekDesk.Control.Windows
DesktopBG.Height = this.Height; DesktopBG.Height = this.Height;
this.Topmost = true; this.Topmost = true;
//获取缩放比例
double scale = ScreenUtil.GetScreenScalingFactor();
bgBitmap = new System.Drawing.Bitmap( bgBitmap = new System.Drawing.Bitmap(
Screen.AllScreens[0].Bounds.Width, (int)(Width * scale),
Screen.AllScreens[0].Bounds.Height, (int)(Height * scale),
System.Drawing.Imaging.PixelFormat.Format32bppArgb System.Drawing.Imaging.PixelFormat.Format32bppArgb
); );

View File

@@ -154,7 +154,7 @@ namespace ShowSeconds
//获取实际坐标 windows可能会有缩放 //获取实际坐标 windows可能会有缩放
IntPtr hdc = GetDC(IntPtr.Zero); IntPtr hdc = GetDC(IntPtr.Zero);
double scale = GetScreenScalingFactor(); double scale = ScreenUtil.GetScreenScalingFactor();
x = (int)(x / scale); x = (int)(x / scale);
y = (int)(y / scale); y = (int)(y / scale);
@@ -268,7 +268,7 @@ namespace ShowSeconds
double h = 1080; double h = 1080;
double width = SystemParameters.PrimaryScreenWidth; double width = SystemParameters.PrimaryScreenWidth;
double height = SystemParameters.PrimaryScreenHeight; double height = SystemParameters.PrimaryScreenHeight;
double scale = GetScreenScalingFactor(); double scale = ScreenUtil.GetScreenScalingFactor();
Console.WriteLine("bef:" + w2 / w * width); Console.WriteLine("bef:" + w2 / w * width);
Console.WriteLine("af:" + w2 / w * width * scale); Console.WriteLine("af:" + w2 / w * width * scale);
@@ -347,23 +347,9 @@ namespace ShowSeconds
//####################################################### //#######################################################
public const int HORZRES = 8;
public const int VERTRES = 10;
public const int DESKTOPVERTRES = 117;
public const int DESKTOPHORZRES = 118;
private static double GetScreenScalingFactor()
{
var g = Graphics.FromHwnd(IntPtr.Zero);
IntPtr desktop = g.GetHdc();
var physicalScreenHeight = GetDeviceCaps(desktop, (int)DESKTOPVERTRES);
var screenScalingFactor =
(double)physicalScreenHeight / SystemParameters.PrimaryScreenHeight;
//SystemParameters.PrimaryScreenHeight;
return screenScalingFactor;
}
/// <summary> /// <summary>
@@ -385,7 +371,6 @@ namespace ShowSeconds
public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC); public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("user32")] [DllImport("user32")]
public static extern bool GetCursorPos(out System.Drawing.Point pt); public static extern bool GetCursorPos(out System.Drawing.Point pt);
[DllImport("gdi32")]
static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
} }
} }

View File

@@ -120,5 +120,30 @@ namespace GeekDesk.Util
return screenPixel.GetPixel(0, 0); return screenPixel.GetPixel(0, 0);
} }
[DllImport("gdi32")]
static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
public const int HORZRES = 8;
public const int VERTRES = 10;
public const int DESKTOPVERTRES = 117;
public const int DESKTOPHORZRES = 118;
/// <summary>
/// 获取屏幕缩放比例
/// </summary>
/// <returns></returns>
public static double GetScreenScalingFactor()
{
var g = Graphics.FromHwnd(IntPtr.Zero);
IntPtr desktop = g.GetHdc();
var physicalScreenHeight = GetDeviceCaps(desktop, (int)DESKTOPVERTRES);
var screenScalingFactor =
(double)physicalScreenHeight / SystemParameters.PrimaryScreenHeight;
//SystemParameters.PrimaryScreenHeight;
return screenScalingFactor;
}
} }
} }