Skip to content

Commit

Permalink
Refactor WindowsScreenCapture
Browse files Browse the repository at this point in the history
  • Loading branch information
Neakita committed Sep 16, 2023
1 parent c25805f commit f52099a
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions SightKeeper.Services.Windows/WindowsScreenCapture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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();
Expand All @@ -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;
}

0 comments on commit f52099a

Please sign in to comment.