feat: improve screenshot capture by ensuring proper bitmap management and adjusting texture size

This commit is contained in:
2026-01-16 14:10:01 -05:00
parent 4d7dfffc5c
commit 4e4a57a9b2
+23 -8
View File
@@ -16,6 +16,8 @@ namespace Spectrum.Detection
private IDXGIOutputDuplication? _duplication; private IDXGIOutputDuplication? _duplication;
private ID3D11Texture2D? _stagingTex; private ID3D11Texture2D? _stagingTex;
private Size _desktopSize; private Size _desktopSize;
private Bitmap? _captureBitmap;
private Size _captureBitmapSize;
public bool IsDirectXAvailable { get; private set; } public bool IsDirectXAvailable { get; private set; }
public bool IsInitialized => _device != null && _duplication != null && _stagingTex != null; 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); bounds = Rectangle.Intersect(new Rectangle(Point.Empty, _desktopSize), bounds);
if (bounds.Width <= 0 || bounds.Height <= 0) return null; 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; IDXGIResource? desktopResource = null;
bool frameAcquired = false; bool frameAcquired = false;
try 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.Failure)
{ {
if (result.Code == unchecked((int)Vortice.DXGI.ResultCode.AccessLost) || if (result.Code == unchecked((int)Vortice.DXGI.ResultCode.AccessLost) ||
@@ -115,15 +118,15 @@ namespace Spectrum.Detection
_context!.CopySubresourceRegion( _context!.CopySubresourceRegion(
_stagingTex!, _stagingTex!,
0, 0,
bounds.X, 0,
bounds.Y, 0,
0, 0,
fullTex, fullTex,
0, 0,
new Vortice.Mathematics.Box(bounds.X, bounds.Y, 0, bounds.Right, bounds.Bottom, 1) 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 map = _context.Map(_stagingTex!, 0, MapMode.Read, Vortice.Direct3D11.MapFlags.None);
var bmpData = bmp.LockBits(new Rectangle(0, 0, bounds.Width, bounds.Height), var bmpData = bmp.LockBits(new Rectangle(0, 0, bounds.Width, bounds.Height),
ImageLockMode.WriteOnly, ImageLockMode.WriteOnly,
@@ -133,7 +136,7 @@ namespace Spectrum.Detection
{ {
unsafe unsafe
{ {
byte* srcBase = (byte*)map.DataPointer + bounds.Y * map.RowPitch + bounds.X * 4; byte* srcBase = (byte*)map.DataPointer;
int srcStride = map.RowPitch; int srcStride = map.RowPitch;
int dstStride = bmpData.Stride; int dstStride = bmpData.Stride;
int rowBytes = bounds.Width * 4; int rowBytes = bounds.Width * 4;
@@ -154,7 +157,7 @@ namespace Spectrum.Detection
_context.Unmap(_stagingTex!, 0); _context.Unmap(_stagingTex!, 0);
} }
return bmp; return bmp.Clone(new Rectangle(0, 0, bounds.Width, bounds.Height), PixelFormat.Format32bppArgb);
} }
finally 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); var bmp = new Bitmap(bounds.Width, bounds.Height, PixelFormat.Format32bppArgb);
using var g = Graphics.FromImage(bmp); using var g = Graphics.FromImage(bmp);
@@ -217,6 +230,8 @@ namespace Spectrum.Detection
lock (_sync) lock (_sync)
{ {
DisposeDxgiResources(); DisposeDxgiResources();
_captureBitmap?.Dispose();
_captureBitmap = null;
} }
GC.SuppressFinalize(this); GC.SuppressFinalize(this);
} }