1- using Aimmy2 . Class ;
2- using SharpGen . Runtime ;
3- using System . Diagnostics ;
1+ using System . Diagnostics ;
42using System . Drawing ;
53using System . Drawing . Imaging ;
64using System . Windows ;
75using System . Windows . Threading ;
6+ using Aimmy2 . Class ;
7+ using SharpGen . Runtime ;
88using Visuality ;
99using Vortice . Direct3D ;
1010using Vortice . Direct3D11 ;
@@ -82,7 +82,7 @@ public void InitializeDxgiDuplication()
8282 factory . EnumAdapters1 ( adapterIndex , out var adapter ) . Success ;
8383 adapterIndex ++ )
8484 {
85- // Debug.WriteLine($"\nAdapter {adapterIndex}:");
85+ Debug . WriteLine ( $ "\n Adapter { adapterIndex } :") ;
8686
8787 for ( uint outputIndex = 0 ;
8888 adapter . EnumOutputs ( outputIndex , out var output ) . Success ;
@@ -98,12 +98,13 @@ public void InitializeDxgiDuplication()
9898 outputDesc . DesktopCoordinates . Right - outputDesc . DesktopCoordinates . Left ,
9999 outputDesc . DesktopCoordinates . Bottom - outputDesc . DesktopCoordinates . Top ) ;
100100
101+ Debug . WriteLine ( $ "Output { outputIndex } : DeviceName = '{ outputDesc . DeviceName . TrimEnd ( '\0 ' ) } ', Bounds = { outputBounds } ") ;
102+
101103 // Try different matching strategies
102104 bool nameMatch = currentDisplay ? . DeviceName != null && outputDesc . DeviceName . TrimEnd ( '\0 ' ) == currentDisplay . DeviceName . TrimEnd ( '\0 ' ) ;
103105 bool boundsMatch = currentDisplay ? . Bounds != null && outputBounds . Equals ( currentDisplay . Bounds ) ;
104106
105- // Try matching by bounds only as a fallback
106- if ( boundsMatch )
107+ if ( nameMatch || boundsMatch )
107108 {
108109 targetOutput1 = output1 ;
109110 targetAdapter = adapter ;
@@ -115,13 +116,11 @@ public void InitializeDxgiDuplication()
115116 }
116117
117118 if ( foundTarget ) break ;
118- adapter . Dispose ( ) ;
119119 }
120120
121121 // Fallback to specific display index if not found
122- if ( ! foundTarget ) //targetOutput1 == null || targetAdapter == null
122+ if ( ! foundTarget )
123123 {
124- // Try to find by index
125124 int targetIndex = currentDisplay ? . Index ?? 0 ;
126125 int currentIndex = 0 ;
127126
@@ -135,7 +134,7 @@ public void InitializeDxgiDuplication()
135134 {
136135 if ( currentIndex == targetIndex )
137136 {
138- // Debug.WriteLine($"Found display at index {targetIndex}");
137+ Debug . WriteLine ( $ "Found display at index { targetIndex } ") ;
139138 targetOutput1 = output . QueryInterface < IDXGIOutput1 > ( ) ;
140139 targetAdapter = adapter ;
141140 foundTarget = true ;
@@ -175,8 +174,7 @@ public void InitializeDxgiDuplication()
175174 DriverType . Unknown ,
176175 DeviceCreationFlags . None ,
177176 featureLevels ,
178- out _dxDevice
179- ) ;
177+ out _dxDevice ) ;
180178
181179 if ( result . Failure || _dxDevice == null )
182180 {
@@ -195,17 +193,14 @@ out _dxDevice
195193 _deskDuplication = targetOutput1 . DuplicateOutput ( _dxDevice ) ;
196194 _consecutiveFailures = 0 ; //reset on success
197195
198- targetAdapter . Dispose ( ) ;
199- targetOutput1 . Dispose ( ) ;
196+ Debug . WriteLine ( "DXGI Duplication initialized successfully." ) ;
200197 }
201198 catch ( SharpGenException ex ) when ( ex . ResultCode == Vortice . DXGI . ResultCode . Unsupported || ex . HResult == unchecked ( ( int ) 0x887A0004 ) )
202199 {
203- //DirectX Desktop Duplication not supported
204200 Debug . WriteLine ( $ "DirectX Desktop Duplication not supported: { ex . Message } ") ;
205201 _directXFailedPermanently = true ;
206202 DisposeDxgiResources ( ) ;
207203
208- // Force switch to GDI+
209204 Dictionary . dropdownState [ "Screen Capture Method" ] = "GDI+" ;
210205 _currentCaptureMethod = "GDI+" ;
211206
@@ -279,7 +274,7 @@ out _dxDevice
279274
280275 if ( _consecutiveFailures >= MAX_CONSECUTIVE_FAILURES )
281276 lock ( _displayLock ) { _displayChangesPending = true ; }
282-
277+
283278 return GetCachedFrame ( detectionBox ) ;
284279 }
285280 else if ( result != Result . Ok )
@@ -368,7 +363,7 @@ out _dxDevice
368363
369364 if ( ++ _consecutiveFailures >= MAX_CONSECUTIVE_FAILURES )
370365 lock ( _displayLock ) { _displayChangesPending = true ; }
371-
366+
372367 return GetCachedFrame ( detectionBox ) ;
373368 }
374369 finally
@@ -383,7 +378,7 @@ out _dxDevice
383378 catch { }
384379
385380 desktopResource ? . Dispose ( ) ;
386-
381+
387382 }
388383 }
389384
@@ -529,4 +524,4 @@ public void Dispose()
529524 screenCaptureBitmap ? . Dispose ( ) ;
530525 }
531526 }
532- }
527+ }
0 commit comments