From cce7a1e66397d519f59b73323fbcff314f8ff5ac Mon Sep 17 00:00:00 2001 From: Sophia Date: Tue, 13 Aug 2024 04:25:03 -0700 Subject: [PATCH] Add ClampRectangle method and update D3D11Screen logic Introduced a new private method `ClampRectangle` in the `AIManager` class to ensure rectangles are clamped within screen bounds, preventing out-of-bounds issues. Updated the `D3D11Screen` method to use `ClampRectangle` for adjusting the `detectionBox` parameter. Added a `using` directive for `System.Windows.Threading` to support the use of `Dispatcher`. --- Aimmy2/AILogic/AIManager.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Aimmy2/AILogic/AIManager.cs b/Aimmy2/AILogic/AIManager.cs index 5167e681..e6bf712e 100644 --- a/Aimmy2/AILogic/AIManager.cs +++ b/Aimmy2/AILogic/AIManager.cs @@ -12,6 +12,7 @@ using System.Drawing.Imaging; using System.IO; using System.Windows; +using System.Windows.Threading; using Visuality; using Vortice.Direct3D; using Vortice.Direct3D11; @@ -672,7 +673,12 @@ private void ReinitializeD3D11() // Reinitialize Direct3D device and context InitializeDirect3D(); } - + private Rectangle ClampRectangle(Rectangle rect, int screenWidth, int screenHeight) + { + int x = Math.Max(0, Math.Min(rect.X, screenWidth - rect.Width)); + int y = Math.Max(0, Math.Min(rect.Y, screenHeight - rect.Height)); + return new Rectangle(x, y, rect.Width, rect.Height); + } private Bitmap? D3D11Screen(Rectangle detectionBox) { try @@ -681,7 +687,7 @@ private void ReinitializeD3D11() { throw new InvalidOperationException("Direct3D device or context is not initialized."); } - + detectionBox = ClampRectangle(detectionBox, ScreenWidth, ScreenHeight); var result = _outputDuplication.AcquireNextFrame(500, out var frameInfo, out var desktopResource); if (result != Result.Ok)