From f52099a2c7f3de7c51dee573e6f37171df8e2568 Mon Sep 17 00:00:00 2001 From: KatterMaw Date: Sat, 16 Sep 2023 20:14:17 +0500 Subject: [PATCH] Refactor WindowsScreenCapture --- .../WindowsScreenCapture.cs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/SightKeeper.Services.Windows/WindowsScreenCapture.cs b/SightKeeper.Services.Windows/WindowsScreenCapture.cs index 4b2a5f37..ad7830af 100644 --- a/SightKeeper.Services.Windows/WindowsScreenCapture.cs +++ b/SightKeeper.Services.Windows/WindowsScreenCapture.cs @@ -11,7 +11,14 @@ public sealed class WindowsScreenCapture : ScreenCapture { public WindowsScreenCapture(ScreenBoundsProvider screenBoundsProvider) { - _screenBoundsProvider = screenBoundsProvider; + _screenCenter = new Point( + screenBoundsProvider.MainScreenHorizontalCenter, + screenBoundsProvider.MainScreenVerticalCenter); + } + + public WindowsScreenCapture(Point screenCenter) + { + _screenCenter = screenCenter; } public byte[] Capture() @@ -21,8 +28,8 @@ public byte[] Capture() using var graphics = Graphics.FromImage(windowsBitmap); Point point = new( - _screenBoundsProvider.MainScreenHorizontalCenter - Resolution.Value / 2 - XOffset, - _screenBoundsProvider.MainScreenVerticalCenter - Resolution.Value / 2 - YOffset); + _screenCenter.X - Resolution.Value / 2 - XOffset, + _screenCenter.Y - Resolution.Value / 2 - YOffset); graphics.CopyFromScreen(point, Point.Empty, new Size(Resolution.Value, Resolution.Value)); using MemoryStream stream = new(); @@ -43,12 +50,12 @@ public bool CanCapture get { if (Game == null) return true; - Process? process = Process.GetProcessesByName(Game.ProcessName) + var process = Process.GetProcessesByName(Game.ProcessName) .FirstOrDefault(process => process.MainWindowHandle > 0); if (process == null) return false; return User32.GetForegroundWindow() == process.MainWindowHandle; } } - private readonly ScreenBoundsProvider _screenBoundsProvider; + private readonly Point _screenCenter; }