Skip to content

Commit bd0d49f

Browse files
committed
Added check to downscale limit and automatically reduced it if the captured region is not big enough to scale down that much (could cause crashed)
1 parent 8d0a5f6 commit bd0d49f

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

ScreenCapture.NET/DirectX/DX11ScreenCapture.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ public CaptureZone RegisterCaptureZone(int x, int y, int width, int height, int
300300

301301
int unscaledWidth = width;
302302
int unscaledHeight = height;
303-
(width, height) = CalculateScaledSize(unscaledWidth, unscaledHeight, downscaleLevel);
303+
(width, height, downscaleLevel) = CalculateScaledSize(unscaledWidth, unscaledHeight, downscaleLevel);
304304

305305
byte[] buffer = new byte[width * height * BPP];
306306

@@ -354,7 +354,7 @@ public void UpdateCaptureZone(CaptureZone captureZone, int? x = null, int? y = n
354354
//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.
355355
if ((width != null) || (height != null) || (downscaleLevel != null))
356356
{
357-
(int newWidth, int newHeight) = CalculateScaledSize(newUnscaledWidth, newUnscaledHeight, newDownscaleLevel);
357+
(int newWidth, int newHeight, newDownscaleLevel) = CalculateScaledSize(newUnscaledWidth, newUnscaledHeight, newDownscaleLevel);
358358
lock (_captureZones)
359359
{
360360
UnregisterCaptureZone(captureZone);
@@ -371,19 +371,25 @@ public void UpdateCaptureZone(CaptureZone captureZone, int? x = null, int? y = n
371371
}
372372
}
373373

374-
private (int width, int height) CalculateScaledSize(int width, int height, int downscaleLevel)
374+
private (int width, int height, int downscaleLevel) CalculateScaledSize(int width, int height, int downscaleLevel)
375375
{
376376
if (downscaleLevel > 0)
377377
for (int i = 0; i < downscaleLevel; i++)
378378
{
379+
if ((width <= 1) && (height <= 1))
380+
{
381+
downscaleLevel = i;
382+
break;
383+
}
384+
379385
width /= 2;
380386
height /= 2;
381387
}
382388

383389
if (width < 1) width = 1;
384390
if (height < 1) height = 1;
385391

386-
return (width, height);
392+
return (width, height, downscaleLevel);
387393
}
388394

389395
private void ValidateCaptureZoneAndThrow(int x, int y, int width, int height)

0 commit comments

Comments
 (0)