Skip to content

Commit 520c4a9

Browse files
committed
Added functionality to reposition CaptureZones, without having to recreate the CaptureZone from scratch.
1 parent e9d0927 commit 520c4a9

File tree

3 files changed

+40
-11
lines changed

3 files changed

+40
-11
lines changed

ScreenCapture.NET/DirectX/DX11ScreenCapture.cs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -203,14 +203,7 @@ private void UpdateZones()
203203
/// <inheritdoc />
204204
public CaptureZone RegisterCaptureZone(int x, int y, int width, int height, int downscaleLevel = 0)
205205
{
206-
if (_device == null) throw new ApplicationException("ScreenCapture isn't initialized.");
207-
208-
if (x < 0) throw new ArgumentException("x < 0");
209-
if (y < 0) throw new ArgumentException("y < 0");
210-
if (width <= 0) throw new ArgumentException("with <= 0");
211-
if (height <= 0) throw new ArgumentException("height <= 0");
212-
if ((x + width) > Display.Width) throw new ArgumentException("x + width > Display width");
213-
if ((y + height) > Display.Height) throw new ArgumentException("y + height > Display height");
206+
CaptureZoneValidityCheck(x, y, width, height);
214207

215208
int unscaledWidth = width;
216209
int unscaledHeight = height;
@@ -252,6 +245,33 @@ public bool UnregisterCaptureZone(CaptureZone captureZone)
252245
}
253246
}
254247

248+
/// <inheritdoc />
249+
public void RepositionCaptureZone(CaptureZone captureZone, int x, int y)
250+
{
251+
CaptureZoneValidityCheck(x, y, captureZone.UnscaledWidth, captureZone.UnscaledHeight);
252+
253+
lock (_captureZones)
254+
{
255+
if (!_captureZones.ContainsKey(captureZone))
256+
throw new ArgumentException("Non registered CaptureZone", nameof(captureZone));
257+
}
258+
259+
captureZone.X = x;
260+
captureZone.Y = y;
261+
}
262+
263+
private void CaptureZoneValidityCheck(int x, int y, int width, int height)
264+
{
265+
if (_device == null) throw new ApplicationException("ScreenCapture isn't initialized.");
266+
267+
if (x < 0) throw new ArgumentException("x < 0");
268+
if (y < 0) throw new ArgumentException("y < 0");
269+
if (width <= 0) throw new ArgumentException("with <= 0");
270+
if (height <= 0) throw new ArgumentException("height <= 0");
271+
if ((x + width) > Display.Width) throw new ArgumentException("x + width > Display width");
272+
if ((y + height) > Display.Height) throw new ArgumentException("y + height > Display height");
273+
}
274+
255275
private void InitializeCaptureZone(in CaptureZone captureZone)
256276
{
257277
Texture2DDescription stagingTextureDesc = new()

ScreenCapture.NET/IScreenCapture.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,19 @@ public interface IScreenCapture : IDisposable
3737
/// <summary>
3838
/// Removes the given <see cref="CaptureScreen"/> from the <see cref="IScreenCapture"/>.
3939
/// </summary>
40-
/// <param name="captureZone">The previosly registered <see cref="CaptureScreen"/>.</param>
40+
/// <param name="captureZone">The previously registered <see cref="CaptureScreen"/>.</param>
4141
/// <returns><c>true</c> if the <see cref="CaptureScreen"/> was successfully removed; otherwise, <c>false</c>.</returns>
4242
bool UnregisterCaptureZone(CaptureZone captureZone);
4343

44+
/// <summary>
45+
/// Updates the position of the given <see cref="CaptureScreen"/>.
46+
/// </summary>
47+
/// <param name="captureZone">The previously registered <see cref="CaptureScreen"/>.</param>
48+
/// <param name="x">The new x-location of the region on the screen.</param>
49+
/// <param name="y">The new y-location of the region on the screen</param>
50+
/// <returns><c>true</c> if the <see cref="CaptureScreen"/> was successfully repositioned; otherwise, <c>false</c>.</returns>
51+
void RepositionCaptureZone(CaptureZone captureZone, int x, int y);
52+
4453
/// <summary>
4554
/// Restarts the <see cref="IScreenCapture"/>.
4655
/// </summary>

ScreenCapture.NET/Model/CaptureZone.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ public sealed class CaptureZone
1919
/// <summary>
2020
/// Gets the x-location of the region on the screen.
2121
/// </summary>
22-
public int X { get; }
22+
public int X { get; internal set; }
2323

2424
/// <summary>
2525
/// Gets the y-location of the region on the screen.
2626
/// </summary>
27-
public int Y { get; }
27+
public int Y { get; internal set; }
2828

2929
/// <summary>
3030
/// Gets the width of the region on the screen.

0 commit comments

Comments
 (0)