From 520c4a98fcdc5b760eabc36e82d52d72ef0ee184 Mon Sep 17 00:00:00 2001 From: Igneom Date: Thu, 28 Apr 2022 00:42:23 -0400 Subject: [PATCH] Added functionality to reposition CaptureZones, without having to recreate the CaptureZone from scratch. --- .../DirectX/DX11ScreenCapture.cs | 36 ++++++++++++++----- ScreenCapture.NET/IScreenCapture.cs | 11 +++++- ScreenCapture.NET/Model/CaptureZone.cs | 4 +-- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/ScreenCapture.NET/DirectX/DX11ScreenCapture.cs b/ScreenCapture.NET/DirectX/DX11ScreenCapture.cs index d0434b8..e7e39ff 100644 --- a/ScreenCapture.NET/DirectX/DX11ScreenCapture.cs +++ b/ScreenCapture.NET/DirectX/DX11ScreenCapture.cs @@ -203,14 +203,7 @@ namespace ScreenCapture.NET /// public CaptureZone RegisterCaptureZone(int x, int y, int width, int height, int downscaleLevel = 0) { - if (_device == null) throw new ApplicationException("ScreenCapture isn't initialized."); - - if (x < 0) throw new ArgumentException("x < 0"); - if (y < 0) throw new ArgumentException("y < 0"); - if (width <= 0) throw new ArgumentException("with <= 0"); - if (height <= 0) throw new ArgumentException("height <= 0"); - if ((x + width) > Display.Width) throw new ArgumentException("x + width > Display width"); - if ((y + height) > Display.Height) throw new ArgumentException("y + height > Display height"); + CaptureZoneValidityCheck(x, y, width, height); int unscaledWidth = width; int unscaledHeight = height; @@ -252,6 +245,33 @@ namespace ScreenCapture.NET } } + /// + public void RepositionCaptureZone(CaptureZone captureZone, int x, int y) + { + CaptureZoneValidityCheck(x, y, captureZone.UnscaledWidth, captureZone.UnscaledHeight); + + lock (_captureZones) + { + if (!_captureZones.ContainsKey(captureZone)) + throw new ArgumentException("Non registered CaptureZone", nameof(captureZone)); + } + + captureZone.X = x; + captureZone.Y = y; + } + + private void CaptureZoneValidityCheck(int x, int y, int width, int height) + { + if (_device == null) throw new ApplicationException("ScreenCapture isn't initialized."); + + if (x < 0) throw new ArgumentException("x < 0"); + if (y < 0) throw new ArgumentException("y < 0"); + if (width <= 0) throw new ArgumentException("with <= 0"); + if (height <= 0) throw new ArgumentException("height <= 0"); + if ((x + width) > Display.Width) throw new ArgumentException("x + width > Display width"); + if ((y + height) > Display.Height) throw new ArgumentException("y + height > Display height"); + } + private void InitializeCaptureZone(in CaptureZone captureZone) { Texture2DDescription stagingTextureDesc = new() diff --git a/ScreenCapture.NET/IScreenCapture.cs b/ScreenCapture.NET/IScreenCapture.cs index 1fc80f7..7107c35 100644 --- a/ScreenCapture.NET/IScreenCapture.cs +++ b/ScreenCapture.NET/IScreenCapture.cs @@ -37,10 +37,19 @@ namespace ScreenCapture.NET /// /// Removes the given from the . /// - /// The previosly registered . + /// The previously registered . /// true if the was successfully removed; otherwise, false. bool UnregisterCaptureZone(CaptureZone captureZone); + /// + /// Updates the position of the given . + /// + /// The previously registered . + /// The new x-location of the region on the screen. + /// The new y-location of the region on the screen + /// true if the was successfully repositioned; otherwise, false. + void RepositionCaptureZone(CaptureZone captureZone, int x, int y); + /// /// Restarts the . /// diff --git a/ScreenCapture.NET/Model/CaptureZone.cs b/ScreenCapture.NET/Model/CaptureZone.cs index dbbc48d..83ff803 100644 --- a/ScreenCapture.NET/Model/CaptureZone.cs +++ b/ScreenCapture.NET/Model/CaptureZone.cs @@ -19,12 +19,12 @@ namespace ScreenCapture.NET /// /// Gets the x-location of the region on the screen. /// - public int X { get; } + public int X { get; internal set; } /// /// Gets the y-location of the region on the screen. /// - public int Y { get; } + public int Y { get; internal set; } /// /// Gets the width of the region on the screen.