From 4e4a57a9b21f6698aa2fe8eabf47c9288e016dca Mon Sep 17 00:00:00 2001 From: whoswhip Date: Fri, 16 Jan 2026 14:10:01 -0500 Subject: [PATCH] feat: improve screenshot capture by ensuring proper bitmap management and adjusting texture size --- Spectrum/Detection/CaptureManager.cs | 31 +++++++++++++++++++++------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/Spectrum/Detection/CaptureManager.cs b/Spectrum/Detection/CaptureManager.cs index 601e669..d5fa209 100644 --- a/Spectrum/Detection/CaptureManager.cs +++ b/Spectrum/Detection/CaptureManager.cs @@ -16,6 +16,8 @@ namespace Spectrum.Detection private IDXGIOutputDuplication? _duplication; private ID3D11Texture2D? _stagingTex; private Size _desktopSize; + private Bitmap? _captureBitmap; + private Size _captureBitmapSize; public bool IsDirectXAvailable { get; private set; } public bool IsInitialized => _device != null && _duplication != null && _stagingTex != null; @@ -92,13 +94,14 @@ namespace Spectrum.Detection bounds = Rectangle.Intersect(new Rectangle(Point.Empty, _desktopSize), bounds); if (bounds.Width <= 0 || bounds.Height <= 0) return null; - EnsureStagingTexture(_desktopSize); + EnsureStagingTexture(new Size(bounds.Width, bounds.Height)); + EnsureCaptureBitmap(new Size(bounds.Width, bounds.Height)); IDXGIResource? desktopResource = null; bool frameAcquired = false; try { - var result = _duplication!.AcquireNextFrame(16, out var frameInfo, out desktopResource); + var result = _duplication!.AcquireNextFrame(0, out var frameInfo, out desktopResource); if (result.Failure) { if (result.Code == unchecked((int)Vortice.DXGI.ResultCode.AccessLost) || @@ -115,15 +118,15 @@ namespace Spectrum.Detection _context!.CopySubresourceRegion( _stagingTex!, 0, - bounds.X, - bounds.Y, + 0, + 0, 0, fullTex, 0, new Vortice.Mathematics.Box(bounds.X, bounds.Y, 0, bounds.Right, bounds.Bottom, 1) ); - var bmp = new Bitmap(bounds.Width, bounds.Height, PixelFormat.Format32bppArgb); + var bmp = _captureBitmap!; var map = _context.Map(_stagingTex!, 0, MapMode.Read, Vortice.Direct3D11.MapFlags.None); var bmpData = bmp.LockBits(new Rectangle(0, 0, bounds.Width, bounds.Height), ImageLockMode.WriteOnly, @@ -133,7 +136,7 @@ namespace Spectrum.Detection { unsafe { - byte* srcBase = (byte*)map.DataPointer + bounds.Y * map.RowPitch + bounds.X * 4; + byte* srcBase = (byte*)map.DataPointer; int srcStride = map.RowPitch; int dstStride = bmpData.Stride; int rowBytes = bounds.Width * 4; @@ -154,7 +157,7 @@ namespace Spectrum.Detection _context.Unmap(_stagingTex!, 0); } - return bmp; + return bmp.Clone(new Rectangle(0, 0, bounds.Width, bounds.Height), PixelFormat.Format32bppArgb); } finally { @@ -190,7 +193,17 @@ namespace Spectrum.Detection }); } - public Bitmap? CaptureScreenshotGdi(Rectangle bounds) + private void EnsureCaptureBitmap(Size size) + { + if (_captureBitmap != null && _captureBitmapSize.Width == size.Width && _captureBitmapSize.Height == size.Height) + return; + + _captureBitmap?.Dispose(); + _captureBitmap = new Bitmap(size.Width, size.Height, PixelFormat.Format32bppArgb); + _captureBitmapSize = size; + } + + public static Bitmap? CaptureScreenshotGdi(Rectangle bounds) { var bmp = new Bitmap(bounds.Width, bounds.Height, PixelFormat.Format32bppArgb); using var g = Graphics.FromImage(bmp); @@ -217,6 +230,8 @@ namespace Spectrum.Detection lock (_sync) { DisposeDxgiResources(); + _captureBitmap?.Dispose(); + _captureBitmap = null; } GC.SuppressFinalize(this); }