mirror of
https://github.com/DarthAffe/ScreenCapture.NET.git
synced 2025-12-13 05:48:39 +00:00
Changed RepositionCaptureZone to UpdateCaptureZone and handled all kinds of changes
This commit is contained in:
parent
520c4a98fc
commit
dee0c096a6
@ -203,21 +203,13 @@ namespace ScreenCapture.NET
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public CaptureZone RegisterCaptureZone(int x, int y, int width, int height, int downscaleLevel = 0)
|
public CaptureZone RegisterCaptureZone(int x, int y, int width, int height, int downscaleLevel = 0)
|
||||||
{
|
{
|
||||||
CaptureZoneValidityCheck(x, y, width, height);
|
ValidateCaptureZoneAndThrow(x, y, width, height);
|
||||||
|
|
||||||
int unscaledWidth = width;
|
int unscaledWidth = width;
|
||||||
int unscaledHeight = height;
|
int unscaledHeight = height;
|
||||||
if (downscaleLevel > 0)
|
(width, height) = CalculateScaledSize(unscaledWidth, unscaledHeight, downscaleLevel);
|
||||||
for (int i = 0; i < downscaleLevel; i++)
|
|
||||||
{
|
|
||||||
width /= 2;
|
|
||||||
height /= 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (width < 1) width = 1;
|
byte[] buffer = new byte[width * height * BPP];
|
||||||
if (height < 1) height = 1;
|
|
||||||
|
|
||||||
byte[] buffer = new byte[width * height * 4];
|
|
||||||
|
|
||||||
CaptureZone captureZone = new(_indexCounter++, x, y, width, height, BPP, downscaleLevel, unscaledWidth, unscaledHeight, buffer);
|
CaptureZone captureZone = new(_indexCounter++, x, y, width, height, BPP, downscaleLevel, unscaledWidth, unscaledHeight, buffer);
|
||||||
lock (_captureZones)
|
lock (_captureZones)
|
||||||
@ -246,21 +238,59 @@ namespace ScreenCapture.NET
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public void RepositionCaptureZone(CaptureZone captureZone, int x, int y)
|
public void UpdateCaptureZone(CaptureZone captureZone, int? x = null, int? y = null, int? width = null, int? height = null, int? downscaleLevel = null)
|
||||||
{
|
{
|
||||||
CaptureZoneValidityCheck(x, y, captureZone.UnscaledWidth, captureZone.UnscaledHeight);
|
lock (_captureZones)
|
||||||
|
if (!_captureZones.ContainsKey(captureZone))
|
||||||
|
throw new ArgumentException("The capture zone is not registered to this ScreenCapture", nameof(captureZone));
|
||||||
|
|
||||||
|
int newX = x ?? captureZone.X;
|
||||||
|
int newY = y ?? captureZone.Y;
|
||||||
|
int newUnscaledWidth = width ?? captureZone.UnscaledWidth;
|
||||||
|
int newUnscaledHeight = height ?? captureZone.UnscaledHeight;
|
||||||
|
int newDownscaleLevel = downscaleLevel ?? captureZone.DownscaleLevel;
|
||||||
|
|
||||||
|
ValidateCaptureZoneAndThrow(newX, newY, newUnscaledWidth, newUnscaledHeight);
|
||||||
|
|
||||||
|
captureZone.X = newX;
|
||||||
|
captureZone.Y = newY;
|
||||||
|
|
||||||
|
//TODO DarthAffe 01.05.2022: For now just reinitialize the zone in that case, but this could be optimized to only recreate the textures needed.
|
||||||
|
if ((width != null) || (height != null) || (downscaleLevel != null))
|
||||||
|
{
|
||||||
|
(int newWidth, int newHeight) = CalculateScaledSize(newUnscaledWidth, newUnscaledHeight, newDownscaleLevel);
|
||||||
lock (_captureZones)
|
lock (_captureZones)
|
||||||
{
|
{
|
||||||
if (!_captureZones.ContainsKey(captureZone))
|
UnregisterCaptureZone(captureZone);
|
||||||
throw new ArgumentException("Non registered CaptureZone", nameof(captureZone));
|
|
||||||
|
captureZone.UnscaledWidth = newUnscaledWidth;
|
||||||
|
captureZone.UnscaledHeight = newUnscaledHeight;
|
||||||
|
captureZone.Width = newWidth;
|
||||||
|
captureZone.Height = newHeight;
|
||||||
|
captureZone.DownscaleLevel = newDownscaleLevel;
|
||||||
|
captureZone.Buffer = new byte[newWidth * newHeight * BPP];
|
||||||
|
|
||||||
|
InitializeCaptureZone(captureZone);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
captureZone.X = x;
|
private (int width, int height) CalculateScaledSize(int width, int height, int downscaleLevel)
|
||||||
captureZone.Y = y;
|
{
|
||||||
|
if (downscaleLevel > 0)
|
||||||
|
for (int i = 0; i < downscaleLevel; i++)
|
||||||
|
{
|
||||||
|
width /= 2;
|
||||||
|
height /= 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void CaptureZoneValidityCheck(int x, int y, int width, int height)
|
if (width < 1) width = 1;
|
||||||
|
if (height < 1) height = 1;
|
||||||
|
|
||||||
|
return (width, height);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ValidateCaptureZoneAndThrow(int x, int y, int width, int height)
|
||||||
{
|
{
|
||||||
if (_device == null) throw new ApplicationException("ScreenCapture isn't initialized.");
|
if (_device == null) throw new ApplicationException("ScreenCapture isn't initialized.");
|
||||||
|
|
||||||
|
|||||||
@ -27,7 +27,7 @@ namespace ScreenCapture.NET
|
|||||||
/// Creates a new <see cref="CaptureScreen"/> for this <see cref="IScreenCapture"/>.
|
/// Creates a new <see cref="CaptureScreen"/> for this <see cref="IScreenCapture"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="x">The x-location of the region to capture (must be >= 0 and < screen-width).</param>
|
/// <param name="x">The x-location of the region to capture (must be >= 0 and < screen-width).</param>
|
||||||
/// <param name="y">The x-location of the region to capture (must be >= 0 and < screen-height).</param>
|
/// <param name="y">The y-location of the region to capture (must be >= 0 and < screen-height).</param>
|
||||||
/// <param name="width">The width of the region to capture (must be >= 0 and this + x must be <= screen-width).</param>
|
/// <param name="width">The width of the region to capture (must be >= 0 and this + x must be <= screen-width).</param>
|
||||||
/// <param name="height">The height of the region to capture (must be >= 0 and this + y must be <= screen-height).</param>
|
/// <param name="height">The height of the region to capture (must be >= 0 and this + y must be <= screen-height).</param>
|
||||||
/// <param name="downscaleLevel">The level of downscaling applied to the image of this region before copying to local memory. The calculation is (width and height)/2^downscaleLevel.</param>
|
/// <param name="downscaleLevel">The level of downscaling applied to the image of this region before copying to local memory. The calculation is (width and height)/2^downscaleLevel.</param>
|
||||||
@ -42,13 +42,18 @@ namespace ScreenCapture.NET
|
|||||||
bool UnregisterCaptureZone(CaptureZone captureZone);
|
bool UnregisterCaptureZone(CaptureZone captureZone);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Updates the position of the given <see cref="CaptureScreen"/>.
|
/// Updates the the given <see cref="CaptureScreen"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// <c>null</c>-parameters are ignored and not changed.
|
||||||
|
/// </remarks>
|
||||||
/// <param name="captureZone">The previously registered <see cref="CaptureScreen"/>.</param>
|
/// <param name="captureZone">The previously registered <see cref="CaptureScreen"/>.</param>
|
||||||
/// <param name="x">The new x-location of the region on the screen.</param>
|
/// <param name="x">The new x-location of the region to capture (must be >= 0 and < screen-width).</param>
|
||||||
/// <param name="y">The new y-location of the region on the screen</param>
|
/// <param name="y">The new y-location of the region to capture (must be >= 0 and < screen-height).</param>
|
||||||
/// <returns><c>true</c> if the <see cref="CaptureScreen"/> was successfully repositioned; otherwise, <c>false</c>.</returns>
|
/// <param name="width">The width of the region to capture (must be >= 0 and this + x must be <= screen-width).</param>
|
||||||
void RepositionCaptureZone(CaptureZone captureZone, int x, int y);
|
/// <param name="height">The new height of the region to capture (must be >= 0 and this + y must be <= screen-height).</param>
|
||||||
|
/// <param name="downscaleLevel">The new level of downscaling applied to the image of this region before copying to local memory. The calculation is (width and height)/2^downscaleLevel.</param>
|
||||||
|
void UpdateCaptureZone(CaptureZone captureZone, int? x = null, int? y = null, int? width = null, int? height = null, int? downscaleLevel = null);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Restarts the <see cref="IScreenCapture"/>.
|
/// Restarts the <see cref="IScreenCapture"/>.
|
||||||
|
|||||||
@ -27,29 +27,29 @@ namespace ScreenCapture.NET
|
|||||||
public int Y { get; internal set; }
|
public int Y { get; internal set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the width of the region on the screen.
|
/// Gets the width of the captured region.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int Width { get; }
|
public int Width { get; internal set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the height of the region on the screen.
|
/// Gets the height of the captured region.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int Height { get; }
|
public int Height { get; internal set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the level of downscaling applied to the image of this region before copying to local memory. The calculation is (width and height)/2^downscaleLevel.
|
/// Gets the level of downscaling applied to the image of this region before copying to local memory. The calculation is (width and height)/2^downscaleLevel.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int DownscaleLevel { get; }
|
public int DownscaleLevel { get; internal set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the original width of the region (this equals <see cref="Width"/> if <see cref="DownscaleLevel"/> is 0).
|
/// Gets the original width of the region (this equals <see cref="Width"/> if <see cref="DownscaleLevel"/> is 0).
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int UnscaledWidth { get; }
|
public int UnscaledWidth { get; internal set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the original height of the region (this equals <see cref="Height"/> if <see cref="DownscaleLevel"/> is 0).
|
/// Gets the original height of the region (this equals <see cref="Height"/> if <see cref="DownscaleLevel"/> is 0).
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int UnscaledHeight { get; }
|
public int UnscaledHeight { get; internal set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the amount of bytes per pixel in the image (most likely 3 [RGB] or 4 [ARGB]).
|
/// Gets the amount of bytes per pixel in the image (most likely 3 [RGB] or 4 [ARGB]).
|
||||||
@ -64,7 +64,7 @@ namespace ScreenCapture.NET
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the buffer containing the image data. Format depends on the specific capture but is most likely BGRA32.
|
/// Gets the buffer containing the image data. Format depends on the specific capture but is most likely BGRA32.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public byte[] Buffer { get; }
|
public byte[] Buffer { get; internal set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the config for black-bar detection.
|
/// Gets the config for black-bar detection.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user