### Example Usage of X11ScreenCaptureService Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/X11ScreenCaptureService.md Demonstrates how to initialize X11ScreenCaptureService, get graphics cards and displays, capture a screen, and dispose of the service. This example is for Linux systems only and requires the LINUX preprocessor directive. ```csharp // Linux only #if LINUX var service = new X11ScreenCaptureService(); try { var card = service.GetGraphicsCards().First(); var display = service.GetDisplays(card).First(); var capture = service.GetScreenCapture(display); if (capture.CaptureScreen()) { Console.WriteLine($"Captured: {display.Width}x{display.Height}"); } } finally { service.Dispose(); } #endif ``` -------------------------------- ### GetScreenCapture(Display) Example Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/IScreenCaptureService.md Illustrates creating an IScreenCapture instance for a specific display. This involves getting a display object first and then calling CaptureScreen() on the resulting capture instance. ```csharp IScreenCaptureService service = new DX11ScreenCaptureService(); GraphicsCard card = service.GetGraphicsCards().First(); Display display = service.GetDisplays(card).First(); IScreenCapture capture = service.GetScreenCapture(display); if (capture.CaptureScreen()) { Console.WriteLine("Screen captured successfully"); } capture.Dispose(); ``` -------------------------------- ### GetGraphicsCards() Example Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/IScreenCaptureService.md Demonstrates how to retrieve a list of all available graphics cards on the system using the IScreenCaptureService. Requires an initialized service instance. ```csharp IScreenCaptureService service = new DX11ScreenCaptureService(); IEnumerable cards = service.GetGraphicsCards(); foreach (GraphicsCard card in cards) { Console.WriteLine($"Card {card.Index}: {card.Name}"); } ``` -------------------------------- ### GetDisplays(GraphicsCard) Example Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/IScreenCaptureService.md Shows how to get all displays connected to a specific graphics card. This method requires a GraphicsCard object obtained from GetGraphicsCards(). ```csharp IScreenCaptureService service = new DX11ScreenCaptureService(); GraphicsCard card = service.GetGraphicsCards().First(); IEnumerable displays = service.GetDisplays(card); foreach (Display display in displays) { Console.WriteLine($"Display {display.Index}: {display.Width}x{display.Height}"); } ``` -------------------------------- ### Instantiating CaptureZone Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/CaptureZone.md Demonstrates how to obtain a CaptureZone instance, typically by casting from an ICaptureZone obtained via IScreenCapture.RegisterCaptureZone. This example shows obtaining a typed CaptureZone. ```csharp IScreenCapture capture = screenCaptureService.GetScreenCapture(display); ICaptureZone zone = capture.RegisterCaptureZone(0, 0, 640, 480); CaptureZone typedZone = zone as CaptureZone; ``` ```csharp DX11ScreenCapture capture = screenCaptureService.GetScreenCapture(display) as DX11ScreenCapture; CaptureZone zone = capture.RegisterCaptureZone(0, 0, 640, 480); ``` -------------------------------- ### Initialize X11ScreenCaptureService Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/X11ScreenCaptureService.md Initializes a new instance of the X11 screen capture service. Requires a Linux/Unix system with an X11/X.Org server running and libX11 installed. May throw DllNotFoundException if libX11 is not available. ```csharp public X11ScreenCaptureService() ``` -------------------------------- ### X11ScreenCaptureService Constructor Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/X11ScreenCaptureService.md Initializes a new instance of the X11 screen capture service. This requires a Linux/Unix system with an X11/X.Org server running and the libX11 library installed. ```APIDOC ## X11ScreenCaptureService() ### Description Initializes a new instance of the X11 screen capture service. ### Requirements - Linux/Unix system - X11/X.Org server running - libX11 installed ### Throws - `DllNotFoundException` — If libX11 is not available. ``` -------------------------------- ### Basic Screen Capture with Interfaces Source: https://github.com/darthaffe/screencapture.net/blob/master/README.md Demonstrates initializing the screen capture service using interfaces, getting display information, registering capture zones (including downscaling), capturing the screen, and accessing pixel data via IImage, IImageRow, and IImageColumn. ```csharp // Create a screen-capture service IScreenCaptureService screenCaptureService = new DX11ScreenCaptureService(); // Get all available graphics cards IEnumerable graphicsCards = screenCaptureService.GetGraphicsCards(); // Get the displays from the graphics card(s) you are interested in IEnumerable displays = screenCaptureService.GetDisplays(graphicsCards.First()); // Create a screen-capture for all screens you want to capture IScreenCapture screenCapture = screenCaptureService.GetScreenCapture(displays.First()); // Register the regions you want to capture on the screen // Capture the whole screen ICaptureZone fullscreen = screenCapture.RegisterCaptureZone(0, 0, screenCapture.Display.Width, screenCapture.Display.Height); // Capture a 100x100 region at the top left and scale it down to 50x50 ICaptureZone topLeft = screenCapture.RegisterCaptureZone(0, 0, 100, 100, downscaleLevel: 1); // Capture the screen // This should be done in a loop on a separate thread as CaptureScreen blocks if the screen is not updated (still image). screenCapture.CaptureScreen(); // Do something with the captured image - e.g. access all pixels (same could be done with topLeft) //Lock the zone to access the data. Remember to dispose the returned disposable to unlock again. using (fullscreen.Lock()) { // You have multiple options now: // 1. Access the raw byte-data ReadOnlySpan rawData = fullscreen.RawBuffer; // 2. Use the provided abstraction to access pixels without having to care about low-level byte handling // Get the image captured for the zone IImage image = fullscreen.Image; // Iterate all pixels of the image foreach (IColor color in image) Console.WriteLine($"A: {color.A}, R: {color.R}, G: {color.G}, B: {color.B}"); // Get the pixel at location (x = 10, y = 20) IColor imageColorExample = image[10, 20]; // Get the first row IImageRow row = image.Rows[0]; // Get the 10th pixel of the row IColor rowColorExample = row[10]; // Get the first column IImageColumn column = image.Columns[0]; // Get the 10th pixel of the column IColor columnColorExample = column[10]; // Cuts a rectangle out of the original image (x = 100, y = 150, width = 400, height = 300) IImage subImage = image[100, 150, 400, 300]; // All of the things above (rows, columns, sub-images) do NOT allocate new memory so they are fast and memory efficient, but for that reason don't provide raw byte access. } ``` -------------------------------- ### GPU-Accelerated Downscaling Example Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/DX11ScreenCapture.md Demonstrates registering a capture zone with GPU-accelerated downscaling. The final size is calculated by dividing the original size by 2 raised to the power of the downscale level. ```csharp // Register a 1920x1080 zone, downscaled to 960x540 (1 level) var zone = capture.RegisterCaptureZone(0, 0, 1920, 1080, downscaleLevel: 1); // zone.Width = 960, zone.Height = 540 ``` -------------------------------- ### Basic Screen Capture Loop Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/README.md Demonstrates how to initialize the DX11ScreenCaptureService, get graphics card and display information, register a capture zone, and continuously capture screen frames in a background task. Accesses pixel data for a specific point. ```csharp using var service = new DX11ScreenCaptureService(); // Get graphics card and display var card = service.GetGraphicsCards().First(); var display = service.GetDisplays(card).First(); // Create capture var capture = service.GetScreenCapture(display); // Register a full-screen capture zone var zone = capture.RegisterCaptureZone(0, 0, display.Width, display.Height); // Capture on background thread await Task.Run(() => { while (isRunning) { if (capture.CaptureScreen()) { using (zone.Lock()) { // Access pixel data var image = zone.Image; var pixel = image[100, 100]; Console.WriteLine($"Pixel: R={pixel.R} G={pixel.G} B={pixel.B}"); } } } }); ``` -------------------------------- ### Getting Capture Zone Coordinates Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/CaptureZone.md Demonstrates how to access the X and Y properties to get the screen coordinates of the capture region. ```csharp Console.WriteLine($"Zone at ({zone.X}, {zone.Y})"); ``` -------------------------------- ### Optimize Gaming/Streaming Capture Performance Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/configuration.md Configure DX11 capture for high performance by setting a short timeout, enabling downscaling, and using manual updates for optimal control. This example demonstrates a typical capture loop. ```csharp var service = new DX11ScreenCaptureService(); var capture = service.GetScreenCapture(display) as DX11ScreenCapture; // Fast frame acquisition capture.Timeout = 100; // Downscale if needed to reduce transfer var zone = capture.RegisterCaptureZone(0, 0, 1920, 1080, downscaleLevel: 1); // Manual control for optimal performance zone.AutoUpdate = true; // Capture loop Task.Run(() => { while (running) { if (capture.CaptureScreen()) { // Process frame immediately } } }); ``` -------------------------------- ### Performing Screen Capture and Processing Frames Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/DX11ScreenCapture.md Provides an example of a capture loop that continuously calls CaptureScreen, locks a registered zone, and processes the captured image data. ```csharp var capture = service.GetScreenCapture(display) as DX11ScreenCapture; var zone = capture.RegisterCaptureZone(0, 0, 1920, 1080); // Capture loop (on background thread) while (true) { if (capture.CaptureScreen()) { using (zone.Lock()) { // Process captured frame IImage image = zone.Image; } } Thread.Sleep(16); // ~60 FPS } ``` -------------------------------- ### Register Capture Zones Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/IScreenCapture.md Shows how to register capture zones for specific regions of the screen. Examples include capturing the full screen and a smaller region that is downscaled. ```csharp var capture = screenCaptureService.GetScreenCapture(display); // Full screen capture ICaptureZone fullscreen = capture.RegisterCaptureZone( 0, 0, capture.Display.Width, capture.Display.Height ); // 100x100 region at top-left, scaled down to 50x50 ICaptureZone topLeft = capture.RegisterCaptureZone( 0, 0, 100, 100, downscaleLevel: 1 ); ``` -------------------------------- ### Get Graphics Cards Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/DX11ScreenCaptureService.md Enumerates all graphics cards accessible via DirectX 11 DXGI. Includes both dedicated and integrated GPUs. ```csharp var service = new DX11ScreenCaptureService(); foreach (var card in service.GetGraphicsCards()) { Console.WriteLine($"{card.Name} (Vendor: 0x{card.VendorId:X}, Device: 0x{card.DeviceId:X})"); } ``` -------------------------------- ### Get Displays for a Graphics Card Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/DX11ScreenCaptureService.md Retrieves all displays connected to a specific graphics card using DXGI. Extracts resolution and rotation from output descriptions. ```csharp var service = new DX11ScreenCaptureService(); var card = service.GetGraphicsCards().First(); foreach (var display in service.GetDisplays(card)) { Console.WriteLine($"Display {display.Index}: {display.Width}x{display.Height} {display.Rotation}"); } ``` -------------------------------- ### Getting Captured Image Dimensions Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/CaptureZone.md Illustrates how to access the Width and Height properties to determine the captured image size after any applied downscaling. ```csharp // If registered as (0, 0, 1280, 720, downscaleLevel: 1) // Width = 640, Height = 360 Console.WriteLine($"Captured size: {zone.Width}x{zone.Height}"); ``` -------------------------------- ### Get Screen Capture for a Display Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/DX11ScreenCaptureService.md Creates or retrieves a cached DirectX 11-based screen capture for a specified display. The service manages the disposal of captures. ```csharp var service = new DX11ScreenCaptureService(); var card = service.GetGraphicsCards().First(); var display = service.GetDisplays(card).First(); var capture = service.GetScreenCapture(display); try { capture.CaptureScreen(); } finally { service.Dispose(); // Disposes all captures } ``` -------------------------------- ### Accessing RefImage with CaptureZone Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/CaptureZone.md Shows how to get a RefImage for fast, read-only pixel access. This method is similar to accessing the Image property but uses reference semantics for performance. Locking the zone is required. ```csharp using (zone.Lock()) { RefImage refImage = zone.RefImage; ColorBGRA pixel = refImage[50, 100]; } ``` -------------------------------- ### Instantiating DX11ScreenCapture Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/DX11ScreenCapture.md Demonstrates how to obtain an instance of DX11ScreenCapture using the DX11ScreenCaptureService. Direct instantiation is not supported. ```csharp IScreenCaptureService service = new DX11ScreenCaptureService(); GraphicsCard card = service.GetGraphicsCards().First(); Display display = service.GetDisplays(card).First(); DX11ScreenCapture capture = service.GetScreenCapture(display) as DX11ScreenCapture; ``` -------------------------------- ### Get Display Information Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/IScreenCapture.md Retrieves the IScreenCapture instance for a display and accesses its properties, such as dimensions. ```csharp IScreenCapture capture = screenCaptureService.GetScreenCapture(display); Console.WriteLine($"Capturing: {capture.Display.Width}x{capture.Display.Height}"); ``` -------------------------------- ### Create X11 Screen Capture Instance Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/X11ScreenCaptureService.md Creates an X11-based screen capture instance for a specified display. ```csharp public X11ScreenCapture GetScreenCapture(Display display) ``` -------------------------------- ### IsUpdateRequested Property Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/CaptureZone.md Gets a boolean value indicating whether an update is requested for the next frame. This is a read-only property. ```APIDOC ## IsUpdateRequested Property ### Description Gets whether an update is requested for the next frame. ### Type `bool` ### Access Read-only ``` -------------------------------- ### Full Screen Capture Workflow with CaptureZone Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/CaptureZone.md Demonstrates the complete lifecycle of using a CaptureZone within a screen capture service. This includes initializing the service, registering a capture zone, performing captures in a loop, accessing pixel data, and unregistering the zone. Keep lock durations minimal for performance. ```csharp using var service = new DX11ScreenCaptureService(); var card = service.GetGraphicsCards().First(); var display = service.GetDisplays(card).First(); var capture = service.GetScreenCapture(display); var zone = capture.RegisterCaptureZone(0, 0, display.Width, display.Height); // Capture loop var captureTask = Task.Run(() => { while (true) { if (capture.CaptureScreen()) { using (zone.Lock()) { var image = zone.Image; var pixel = image[100, 100]; Console.WriteLine($"Pixel at (100,100): B={pixel.B} G={pixel.G} R={pixel.R}"); } } Thread.Sleep(16); // 60 FPS } }); // Later... capture.UnregisterCaptureZone(zone); ``` -------------------------------- ### Initialize DX11ScreenCaptureService Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/DX11ScreenCaptureService.md Initializes a new instance of the DirectX 11 screen capture service. Requires Windows 10 or later and compatible hardware. ```csharp IScreenCaptureService service = new DX11ScreenCaptureService(); // Use service... service.Dispose(); ``` -------------------------------- ### Initialize DX9ScreenCaptureService (Fallback) Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/DX9ScreenCaptureService.md Demonstrates how to initialize the DX9ScreenCaptureService as a fallback when DX11ScreenCaptureService fails. This snippet shows the typical usage pattern for selecting the appropriate screen capture service. ```csharp try { service = new DX11ScreenCaptureService(); } catch { // Fallback to DirectX 9 service = new DX9ScreenCaptureService(); } using (service) { var card = service.GetGraphicsCards().First(); var display = service.GetDisplays(card).First(); var capture = service.GetScreenCapture(display); capture.CaptureScreen(); } ``` -------------------------------- ### DX9ScreenCaptureService Constructor Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/DX9ScreenCaptureService.md Initializes a new instance of the DirectX 9 screen capture service. This constructor may throw a COMException if DirectX 9 cannot be initialized. ```APIDOC ## DX9ScreenCaptureService() ### Description Initializes a new instance of the DirectX 9 screen capture service. ### Signature ```csharp public DX9ScreenCaptureService() ``` ### Throws - `COMException` — If DirectX 9 cannot be initialized. ``` -------------------------------- ### AutoUpdate Property Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/CaptureZone.md Gets or sets a boolean value indicating whether the zone is automatically updated on every frame. When true, the zone updates during `CaptureScreen()`; when false, updates require `RequestUpdate()`. ```APIDOC ## AutoUpdate Property ### Description Gets or sets whether the zone is updated on every frame. If `true`, the zone is updated automatically in `CaptureScreen()`. If `false`, the zone only updates when `RequestUpdate()` is called. ### Type `bool` ### Access Read-write ### Default `true` ### Remarks - When `true`, zone is updated automatically in `CaptureScreen()`. - When `false`, zone only updates via `RequestUpdate()`. ### Example ```csharp zone.AutoUpdate = false; // Manual control capture.CaptureScreen(); zone.RequestUpdate(); capture.CaptureScreen(); // Zone updates on this call ``` ``` -------------------------------- ### Set X Display Environment Variable Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/configuration.md Configure the X display server for Linux environments. This is typically handled by the desktop environment but can be set manually. ```bash # Usually set by desktop environment export DISPLAY=:0 # Single display export DISPLAY=:0.0 # Specific screen export DISPLAY=:1 # Secondary server ``` -------------------------------- ### Getting Typed RefImage with GetRefImage Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/CaptureZone.md Demonstrates using GetRefImage() to obtain a typed reference-based image view. This method performs type checking and throws an ArgumentException if the color format does not match. ```csharp using (zone.Lock()) { // This will throw if zone is not ColorBGRA RefImage refImage = zone.GetRefImage(); } ``` -------------------------------- ### DX11ScreenCaptureService Constructor Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/DX11ScreenCaptureService.md Initializes a new instance of the DirectX 11 screen capture service. This service requires Windows 10 or later and compatible hardware with DirectX 11 support. ```APIDOC ## DX11ScreenCaptureService() ### Description Initializes a new instance of the DirectX 11 screen capture service. ### Signature ```csharp public DX11ScreenCaptureService() ``` ### Returns `DX11ScreenCaptureService` instance ### Throws - `COMException` — If DirectX 11 is not available or cannot be initialized. ### Remarks - Creates an internal DXGI Factory for DirectX operations. - Requires Windows 10 or later. - Requires compatible graphics hardware with DirectX 11 support. ### Example ```csharp IScreenCaptureService service = new DX11ScreenCaptureService(); // Use service... service.Dispose(); ``` ``` -------------------------------- ### Registering Capture Zones Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/DX11ScreenCapture.md Demonstrates registering capture zones with different configurations, including full-screen capture and scaled regions using the downscaleLevel parameter. ```csharp var capture = service.GetScreenCapture(display) as DX11ScreenCapture; // Full screen without scaling var fullScreen = capture.RegisterCaptureZone(0, 0, 1920, 1080); // 1280x720 region scaled to 640x360 (downscaleLevel=1) var scaled = capture.RegisterCaptureZone(0, 0, 1280, 720, downscaleLevel: 1); ``` -------------------------------- ### Increase Threshold for Bar Detection Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/BlackBarDetection.md Demonstrates how to increase the threshold value when removing black bars, useful when bars are not detected due to a threshold that is too low. This example uses a threshold of 30. ```csharp // Try higher thresholds var cropped = image.RemoveBlackBars(threshold: 30); ``` -------------------------------- ### GetScreenCapture(Display) Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/X11ScreenCaptureService.md Creates an X11-based screen capture instance for a specified display. ```APIDOC ## GetScreenCapture(Display display) ### Description Creates an X11-based screen capture for a screen. ### Parameters #### Path Parameters - **display** (Display) - Required - The display to capture from ### Returns `X11ScreenCapture` — A screen capture instance. ``` -------------------------------- ### Manual Pixel Access via RawBuffer Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/CaptureZone.md Illustrates direct pixel access using the RawBuffer and stride calculations. This method provides low-level control but requires careful index management. ```csharp // Access pixel at (x=100, y=50) int pixelIndex = (50 * zone.Stride) + (100 * 4); byte b = zone.RawBuffer[pixelIndex]; byte g = zone.RawBuffer[pixelIndex + 1]; byte r = zone.RawBuffer[pixelIndex + 2]; byte a = zone.RawBuffer[pixelIndex + 3]; // Or more simply: var image = zone.Image; var color = image[100, 50]; ``` -------------------------------- ### GetRefImage() Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/ICaptureZone.md Gets a RefImage for faster pixel access when the color type is known. This method provides better performance than IImage for known color layouts and requires knowledge of the exact color format. ```APIDOC ## GetRefImage() ### Description Gets a `RefImage` for faster pixel access when the color type is known. Provides better performance than `IImage` for known color layouts and requires knowledge of the exact color format. ### Signature ```csharp RefImage GetRefImage() where TColor : struct, IColor ``` ### Type Parameters - **TColor**: The color type (must be `ColorBGRA` or similar). ### Returns - `RefImage`: A reference-based image abstraction. ### Throws - `ArgumentException`: If the requested color format doesn't match the zone's buffer format. ### Remarks - Provides better performance than `IImage` for known color layouts. - Requires knowledge of the exact color format. - No memory allocation; uses direct buffer references. ### Example ```csharp using (zone.Lock()) { RefImage refImage = zone.GetRefImage(); foreach (ColorBGRA color in refImage) { // Direct pixel access byte r = color.R; byte g = color.G; byte b = color.B; byte a = color.A; } } ``` ``` -------------------------------- ### Downscaled Screen Capture Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/README.md Shows how to register a capture zone that captures the full screen but downscales the output to a quarter resolution (2^2). The resulting zone dimensions will be half of the display's width and height. ```csharp // Capture full screen but downscale to 1/4 resolution var zone = capture.RegisterCaptureZone( 0, 0, display.Width, display.Height, downscaleLevel: 2 // 1/4 = 2^2 ); // zone.Width = display.Width / 4 // zone.Height = display.Height / 4 ``` -------------------------------- ### Creating Sub-Images Without Allocation Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/README.md Illustrates how to create sub-images from an existing image without allocating new memory, as sub-images are references. ```csharp // Good: Sub-images are references, not copies var subImage = image[100, 150, 200, 200]; // No allocation ``` -------------------------------- ### Resource Cleanup with DX11ScreenCaptureService Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/README.md Demonstrates two ways to ensure proper disposal of the DX11ScreenCaptureService to release DirectX resources. Use 'using' for automatic disposal or a 'try-finally' block for manual control. ```csharp // Always dispose services using (var service = new DX11ScreenCaptureService()) { // Use service // Automatic disposal of all captures } // Or manually var service = new DX11ScreenCaptureService(); try { // Use service } finally { service.Dispose(); // Releases DirectX resources } ``` -------------------------------- ### GetRefImage() Method Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/CaptureZone.md Gets a reference-based image view with explicit color type checking. This generic method is useful when the color type is dynamic or needs validation, throwing an `ArgumentException` if the requested color format does not match the zone's format. ```APIDOC ## GetRefImage() Method ### Description Gets a reference-based image with explicit color type checking. This is a type-checked alternative to the `RefImage` property, useful when the color type is dynamic or needs validation. ### Signature ```csharp public RefImage GetRefImage() where T : struct, IColor ``` ### Parameters #### Type Parameters - **T** (`type parameter`) - Required - The color type (must match buffer format) ### Returns `RefImage` — A reference-based image view. ### Throws - `ArgumentException` — If the requested color format doesn't match the zone's format. ### Remarks - Type-checked alternative to `RefImage` property. - Useful when the color type is dynamic or needs validation. ### Example ```csharp using (zone.Lock()) { // This will throw if zone is not ColorBGRA RefImage refImage = zone.GetRefImage(); } ``` ``` -------------------------------- ### Capture Screen in a Loop Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/IScreenCapture.md Demonstrates capturing the screen continuously on a background thread. It registers a capture zone and accesses the captured image within a loop, processing it when a capture is successful. ```csharp var capture = screenCaptureService.GetScreenCapture(display); ICaptureZone zone = capture.RegisterCaptureZone(0, 0, 640, 480); // Capture in a background thread await Task.Run(() => { while (true) { if (capture.CaptureScreen()) { using (zone.Lock()) { // Access the captured image IImage image = zone.Image; // Process pixels... } } } }); ``` -------------------------------- ### Decrease Threshold or Limit Sides for Bar Detection Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/BlackBarDetection.md Shows how to decrease the threshold or selectively remove bars from specific sides to prevent content from being incorrectly identified as bars. This example uses a threshold of 5 for all sides, and then a threshold of 20 for left and right only. ```csharp // Lower threshold var cropped = image.RemoveBlackBars(threshold: 5); // Or remove only some sides var cropped2 = image.RemoveBlackBars( threshold: 20, removeLeft: true, removeRight: false ); ``` -------------------------------- ### Service Disposal Pattern Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/DX11ScreenCapture.md Illustrates the recommended pattern for using and disposing of the DX11ScreenCaptureService. Using a 'using' statement ensures that all associated captures are automatically disposed of when the service goes out of scope. ```csharp using (var service = new DX11ScreenCaptureService()) { var capture = service.GetScreenCapture(display); // Use capture... } // All captures automatically disposed ``` -------------------------------- ### CaptureScreen Method Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/DX11ScreenCapture.md Initiates a screen capture using DirectX 11 desktop duplication and returns whether a new frame was successfully captured. ```APIDOC ## Methods ### CaptureScreen() Performs a DirectX 11 desktop duplication frame capture. **Signature:** ```csharp public bool CaptureScreen() ``` **Returns:** `bool` — `true` if a new frame was captured; `false` if no update occurred. **Throws:** - `ObjectDisposedException` — If the capture has been disposed. **Implementation Details:** - Uses `IDXGIOutputDuplication.AcquireNextFrame()` to get the latest frame. - Copies the frame to a GPU texture. - Updates all registered zones with `AutoUpdate=true` or `IsUpdateRequested=true`. - Automatically calls `Restart()` if DirectX errors occur (e.g., access lost). **Example:** ```csharp var capture = service.GetScreenCapture(display) as DX11ScreenCapture; var zone = capture.RegisterCaptureZone(0, 0, 1920, 1080); // Capture loop (on background thread) while (true) { if (capture.CaptureScreen()) { using (zone.Lock()) { // Process captured frame IImage image = zone.Image; } } Thread.Sleep(16); // ~60 FPS } ``` ``` -------------------------------- ### Get Reference Image for Pixel Access Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/ICaptureZone.md Use GetRefImage() to obtain a RefImage for efficient pixel manipulation when the color type is known. This method avoids memory allocation by using direct buffer references and requires the color format to match the zone's buffer. ```csharp using (zone.Lock()) { RefImage refImage = zone.GetRefImage(); foreach (ColorBGRA color in refImage) { // Direct pixel access byte r = color.R; byte g = color.G; byte b = color.B; byte a = color.A; } } ``` -------------------------------- ### Manual Error Recovery for Capture Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/README.md Demonstrates how to manually trigger a restart for the capture service, which can recover from transient errors like device loss or invalid states. ```csharp // Manual recovery: capture.Restart(); ``` -------------------------------- ### Calculating Total Buffer Bytes Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/CaptureZone.md Shows how to use the Stride and Height properties to calculate the total number of bytes in the capture zone's buffer. ```csharp int bytesPerRow = zone.Stride; int totalBytes = zone.Stride * zone.Height; ``` -------------------------------- ### Enumerate Graphics Cards (X11) Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/X11ScreenCaptureService.md Enumerates available graphics cards/displays on the X server. Always returns one virtual 'graphics card' representing the X display. ```csharp public IEnumerable GetGraphicsCards() ``` -------------------------------- ### Verify X11 Access Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/configuration.md Check if the system can access the X11 server. This command should display information about the running X11 server. ```bash xdpyinfo # Should show X11 server info ``` -------------------------------- ### Low-Level Screen Capture without Interfaces Source: https://github.com/darthaffe/screencapture.net/blob/master/README.md Shows a more direct approach to screen capture using concrete types like DX11ScreenCaptureService and CaptureZone, bypassing interfaces for potential performance gains. Accesses pixel data via Image, ImageRow, and ImageColumn abstractions. ```csharp DX11ScreenCaptureService screenCaptureService = new DX11ScreenCaptureService(); IEnumerable graphicsCards = screenCaptureService.GetGraphicsCards(); IEnumerable displays = screenCaptureService.GetDisplays(graphicsCards.First()); DX11ScreenCapture screenCapture = screenCaptureService.GetScreenCapture(displays.First()); CaptureZone fullscreen = screenCapture.RegisterCaptureZone(0, 0, screenCapture.Display.Width, screenCapture.Display.Height); CaptureZone topLeft = screenCapture.RegisterCaptureZone(0, 0, 100, 100, downscaleLevel: 1); screenCapture.CaptureScreen(); using (fullscreen.Lock()) { IImage image = fullscreen.Image; // You can also get a ref image which has a slight performance benefit in some cases // RefImage refImage = image.AsRefImage(); foreach (ColorBGRA color in image) Console.WriteLine($"A: {color.A}, R: {color.R}, G: {color.G}, B: {color.B}"); ColorBGRA imageColorExample = image[10, 20]; ImageRow row = image.Rows[0]; ColorBGRA rowColorExample = row[10]; ImageColumn column = image.Columns[0]; ColorBGRA columnColorExample = column[10]; RefImage subImage = image[100, 150, 400, 300]; } ``` -------------------------------- ### GetGraphicsCards() Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/X11ScreenCaptureService.md Enumerates available graphics cards/displays on the X server. It always returns a single virtual 'graphics card' representing the X server, with the card name being the X display string. ```APIDOC ## GetGraphicsCards() ### Description Enumerates available graphics cards/displays on the X server. ### Returns `IEnumerable` — Single graphics card representing the X display. ### Remarks - Always returns one virtual "graphics card" representing the X server. - The card name is the X display string (e.g., `:0`, `:0.0`). - Vendor ID and Device ID are always 0. ``` -------------------------------- ### Balanced Capture Configuration Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/configuration.md Provides a balanced approach with a default timeout (1000ms) and a sleep interval suitable for ~60 FPS. ```csharp capture.Timeout = 1000; // 1 second (default) // Capture loop while (true) { if (capture.CaptureScreen()) { // Process frame } Thread.Sleep(16); // ~60 FPS } ``` -------------------------------- ### IScreenCapture Interface Overview Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/IScreenCapture.md This snippet provides an overview of the IScreenCapture interface, including its properties, events, and methods. ```APIDOC ## Interface: IScreenCapture **Namespace:** `ScreenCapture.NET` **Source:** `ScreenCapture.NET/Generic/IScreenCapture.cs` ### Properties - **Display** (`Display`): Gets the display being captured. Read-only. ### Events - **Updated** (`EventHandler`): Occurs when a frame capture is completed. Event Args: `IsSuccessful` (bool). ### Methods - **CaptureScreen()**: Attempts to capture the current frame from the display. Returns `bool` (true if successful, false if no update). Blocks if content hasn't changed. Should be called in a loop on a background thread. Raises the `Updated` event. - **RegisterCaptureZone(int x, int y, int width, int height, int downscaleLevel = 0)**: Creates a new capture zone. Returns `ICaptureZone`. Throws `ObjectDisposedException`, `ArgumentException`. - **UnregisterCaptureZone(ICaptureZone captureZone)**: Removes a registered capture zone. Returns `bool` (true if removed, false if not registered). Throws `ObjectDisposedException`. - **UpdateCaptureZone(ICaptureZone captureZone, int? x = null, int? y = null, int? width = null, int? height = null, int? downscaleLevel = null)**: Updates the parameters of an existing capture zone. - **Restart()**: Restarts the screen capture process. ``` -------------------------------- ### Restart Method Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/IScreenCapture.md Restarts the screen capture process, potentially re-initializing resources or re-evaluating display configurations. ```APIDOC ## Method: Restart() ### Description Restarts the screen capture mechanism. This can be useful for re-initializing the capture process, for example, if the display configuration has changed or if the capture has entered an unexpected state. ### Signature ```csharp void Restart() ``` ### Parameters No parameters. ### Returns This method does not return a value. ### Throws - `ObjectDisposedException`: If the screen capture has been disposed. ### Example ```csharp var capture = screenCaptureService.GetScreenCapture(display); // ... perform captures ... // If issues arise or display changes, restart the capture capture.Restart(); ``` ``` -------------------------------- ### GetDisplays(GraphicsCard) Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/X11ScreenCaptureService.md Retrieves all screens from the X server using the provided GraphicsCard object. Note that the graphicsCard parameter is ignored as the method uses the default X display. ```APIDOC ## GetDisplays(GraphicsCard graphicsCard) ### Description Retrieves all screens from the X server. ### Parameters #### Path Parameters - **graphicsCard** (GraphicsCard) - Required - The graphics card (ignored; uses default X display) ### Returns `IEnumerable` — Enumerable of X screens. ### Implementation Notes - Enumerates screens using `XScreenOfDisplay()`. - Device names follow Windows convention for compatibility (e.g., `\.\ ``` -------------------------------- ### Accessing Image Data with CaptureZone Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/CaptureZone.md Demonstrates how to access pixel data using the IImage interface, including individual pixel access, iteration, row access, and obtaining sub-images. This requires locking the zone. ```csharp using (zone.Lock()) { IImage image = zone.Image; // Access individual pixel ColorBGRA pixel = image[10, 20]; // Iterate all pixels foreach (ColorBGRA color in image) { // Process pixel } // Get row ImageRow row = image.Rows[5]; foreach (ColorBGRA color in row) { // Process row pixel } // Get sub-image (no copy) RefImage subImage = image[100, 150, 200, 200]; } ``` -------------------------------- ### Accessing Display Information Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/DX11ScreenCapture.md Shows how to retrieve and display the dimensions of the captured display using the Display property. ```csharp Console.WriteLine($"Capturing: {capture.Display.Width}x{capture.Display.Height}"); ``` -------------------------------- ### Automatic Disposal with 'using' Statement Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/DX11ScreenCaptureService.md This snippet demonstrates the recommended way to use the DX11ScreenCaptureService within a 'using' statement for automatic disposal of DirectX resources. ```csharp using (var service = new DX11ScreenCaptureService()) { // Use service... } // Automatic disposal ``` -------------------------------- ### Verify Hardware Capability with DX11ScreenCaptureService Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/README.md Checks for available graphics cards and their connected displays using the DX11ScreenCaptureService. This is useful for debugging hardware compatibility issues. ```csharp try { var service = new DX11ScreenCaptureService(); var cards = service.GetGraphicsCards().ToList(); if (cards.Count == 0) Console.WriteLine("No graphics cards found!"); foreach (var card in cards) { var displays = service.GetDisplays(card).ToList(); Console.WriteLine($"{card.Name}: {displays.Count} displays"); } } catch (Exception ex) { Console.WriteLine($"Hardware check failed: {ex.Message}"); } ``` -------------------------------- ### Accessing Raw Pixel Data Safely Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/ICaptureZone.md Illustrates the correct way to access the raw pixel buffer of a capture zone using a 'using' statement to ensure the zone is properly locked and unlocked. ```csharp using (zone.Lock()) { IImage image = zone.Image; foreach (IColor color in image) { // Process pixel... } } // Zone is unlocked here ``` -------------------------------- ### GetScreenCapture(Display) Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/DX9ScreenCaptureService.md Creates a screen capture instance using DirectX 9 for a specified display. This is the primary method for obtaining a capture object. ```APIDOC ## GetScreenCapture(Display display) ### Description Creates a DirectX 9-based screen capture. ### Method GET (Conceptual) ### Parameters #### Path Parameters - **display** (Display) - Required - The display to capture from ### Returns `DX9ScreenCapture` — A screen capture instance. ``` -------------------------------- ### Requesting Zone Update Manually Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/CaptureZone.md Explains how to use RequestUpdate() to signal that the zone should be updated on the next screen capture. This is effective only when AutoUpdate is set to false. ```csharp zone.AutoUpdate = false; zone.RequestUpdate(); capture.CaptureScreen(); // Zone updates now ``` -------------------------------- ### Retrieve Displays from X Server Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/X11ScreenCaptureService.md Retrieves all screens from the X server for a given graphics card. The graphicsCard parameter is ignored as it uses the default X display. Device names follow Windows convention for compatibility. ```csharp public IEnumerable GetDisplays(GraphicsCard graphicsCard) ``` -------------------------------- ### Subscribing to Zone Updates Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/ICaptureZone.md Shows how to register an event handler to be notified when a capture zone's pixel data has been updated. ```csharp zone.Updated += (sender, args) => { Console.WriteLine("Zone updated with new pixel data"); }; ``` -------------------------------- ### Restart Screen Capture Process Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/IScreenCapture.md Call this method to reinitialize the screen capture and recreate underlying resources. It can be used to recover from resource loss, such as a driver reset, and automatically preserves all registered capture zones. ```csharp var capture = screenCaptureService.GetScreenCapture(display); // ... later, if capture fails ... capture.Restart(); // Recover from driver/device loss ``` -------------------------------- ### CaptureScreen() Method Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/IScreenCapture.md Attempts to capture the current frame from the display. This method is useful for triggering a screen capture and checking its success. ```APIDOC ## Method: CaptureScreen() ### Description Attempts to capture the current frame from the display. This method blocks if the screen content hasn't changed and should typically be called in a loop on a background thread. It updates registered capture zones and raises the `Updated` event upon completion. ### Signature ```csharp bool CaptureScreen() ``` ### Parameters No parameters. ### Returns - `bool`: `true` if the current frame was captured successfully; `false` if no update occurred. ### Throws - `ObjectDisposedException`: If the screen capture has been disposed. ### Example ```csharp var capture = screenCaptureService.GetScreenCapture(display); ICaptureZone zone = capture.RegisterCaptureZone(0, 0, 640, 480); // Capture in a background thread await Task.Run(() => { while (true) { if (capture.CaptureScreen()) { using (zone.Lock()) { // Access the captured image IImage image = zone.Image; // Process pixels... } } } }); ``` ``` -------------------------------- ### Configure DX11ScreenCapture for Monitoring Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/configuration.md Use this configuration for monitoring applications requiring periodic screenshots. Adjust the timeout for reduced CPU usage and set auto-update to false for manual control. ```csharp var service = new DX11ScreenCaptureService(); var capture = service.GetScreenCapture(display) as DX11ScreenCapture; // Longer timeout reduces CPU usage capture.Timeout = 5000; // Full resolution var zone = capture.RegisterCaptureZone(0, 0, display.Width, display.Height, downscaleLevel: 0); // Manual updates reduce overhead zone.AutoUpdate = false; // Periodic capture Task.Run(() => { while (running) { zone.RequestUpdate(); if (capture.CaptureScreen()) { // Save screenshot } Thread.Sleep(5000); // Every 5 seconds } }); ``` -------------------------------- ### Restart Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/IScreenCapture.md Reinitializes the screen capture, releasing and recreating underlying resources. This method can be called manually to recover from resource loss or is automatically called on DirectX errors. ```APIDOC ## Restart ### Description Reinitializes the screen capture, releasing and recreating underlying resources. ### Method void ### Parameters No parameters. ### Throws - ObjectDisposedException — If the screen capture has been disposed. ### Remarks - Automatically called by `CaptureScreen()` if a DirectX error occurs. - May be called manually to recover from resource loss (e.g., driver reset). - All registered capture zones are preserved. ### Example ```csharp var capture = screenCaptureService.GetScreenCapture(display); // ... later, if capture fails ... capture.Restart(); // Recover from driver/device loss ``` ``` -------------------------------- ### Configure DX11ScreenCapture for Multiple Zones Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/configuration.md This configuration is suitable for capturing multiple distinct regions of a display simultaneously. It demonstrates registering zones for full-screen, downscaled previews, and specific regions of interest. ```csharp var capture = service.GetScreenCapture(display); // Zone 1: Full screen for display var displayZone = capture.RegisterCaptureZone(0, 0, 1920, 1080); // Zone 2: Downscaled preview var previewZone = capture.RegisterCaptureZone(0, 0, 1920, 1080, downscaleLevel: 2); // Zone 3: Small region of interest var roiZone = capture.RegisterCaptureZone(100, 100, 200, 200); // All zones auto-update efficiently capture.CaptureScreen(); // Updates all three ``` -------------------------------- ### GetDisplays Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/DX11ScreenCaptureService.md Retrieves all displays connected to a graphics card via DXGI. This method enumerates DXGI outputs and extracts resolution and rotation from the output description. ```APIDOC ## GetDisplays(GraphicsCard) ### Description Retrieves all displays connected to a graphics card via DXGI. ### Method `GetDisplays` ### Parameters #### Path Parameters - **graphicsCard** (GraphicsCard) - Required - The graphics card to query ### Returns `IEnumerable` — Enumerable of displays on the card. ### Throws - `ObjectDisposedException` — If service has been disposed. ### Implementation Notes - Enumerates DXGI outputs using `IDXGIAdapter1.EnumOutputs()`. - Extracts resolution and rotation from output description. - Device names follow Windows convention (e.g., `\\.\DISPLAY1`). ### Example ```csharp var service = new DX11ScreenCaptureService(); var card = service.GetGraphicsCards().First(); foreach (var display in service.GetDisplays(card)) { Console.WriteLine($"Display {display.Index}: {display.Width}x{display.Height} {display.Rotation}"); } ``` ``` -------------------------------- ### Display Rotation Handling Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/DX11ScreenCapture.md Shows how to check for display rotation. The capture system automatically handles rotated displays and ensures captured pixels match the logical screen orientation. ```csharp if (capture.Display.Rotation == Rotation.Rotation90) { // Pixels are already rotated correctly } ``` -------------------------------- ### Disposing IScreenCaptureService Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/IScreenCaptureService.md Demonstrates the proper disposal of the IScreenCaptureService to release underlying graphics resources. Using a 'using' statement ensures automatic disposal. ```csharp using IScreenCaptureService service = new DX11ScreenCaptureService(); // Use service... // Disposal happens automatically at the end of using block ``` -------------------------------- ### Setting Capture Timeout Source: https://github.com/darthaffe/screencapture.net/blob/master/_autodocs/api-reference/DX11ScreenCapture.md Illustrates how to adjust the Timeout property to control the frame acquisition wait time in milliseconds. This affects responsiveness and CPU usage. ```csharp var capture = service.GetScreenCapture(display) as DX11ScreenCapture; capture.Timeout = 500; // 500ms timeout ```