|
1 | 1 | # ScreenCapture |
2 | 2 | Vortice based Desktop Duplication |
| 3 | + |
| 4 | +## Usage |
| 5 | +```csharp |
| 6 | +// Sets the DPI-awareness of the application - this is required for capturing |
| 7 | +DPIAwareness.Initalize(); |
| 8 | + |
| 9 | +// Create a screen-capture service |
| 10 | +IScreenCaptureService screenCaptureService = new DX11ScreenCaptureService(); |
| 11 | + |
| 12 | +// Get all available graphics cards |
| 13 | +IEnumerable<GraphicsCard> graphicsCards = screenCaptureService.GetGraphicsCards(); |
| 14 | + |
| 15 | +// Get the displays from the graphics card(s) you are interested in |
| 16 | +IEnumerable<Display> displays = screenCaptureService.GetDisplays(graphicsCards.First()); |
| 17 | + |
| 18 | +// Create a screen-capture for all screens you want to capture |
| 19 | +IScreenCapture screenCapture = screenCaptureService.GetScreenCapture(displays.First()); |
| 20 | + |
| 21 | +// Register the regions you want to capture om the screen |
| 22 | +// Capture the whole screen |
| 23 | +CaptureZone fullscreen = screenCapture.RegisterCaptureZone(0, 0, screenCapture.Display.Width, screenCapture.Display.Height); |
| 24 | +// Capture a 100x100 region at the top left and scale it down to 50x50 |
| 25 | +CaptureZone topLeft = screenCapture.RegisterCaptureZone(0, 0, 100, 100, downscaleLevel: 1); |
| 26 | + |
| 27 | +// Capture the screen |
| 28 | +// This should be done in a loop on a seperate thread as CaptureScreen blocks if the screen is not updated (still image) |
| 29 | +screenCapture.CaptureScreen(); |
| 30 | + |
| 31 | +// Do something with the captured image - e.g. access all pixels (same could be done with topLeft) |
| 32 | +// Locking is not neccessary in that case as we're capturing in the same thread, |
| 33 | +// but when using a threaded-approach (which is recommended) it prevents potential tearing of the data in the buffer |
| 34 | +lock (fullscreen.Buffer) |
| 35 | +{ |
| 36 | + // Since the size of the capture can be bigger than the size of our captured region due to size constraints on the GPU, |
| 37 | + // we need to use this for the stride. |
| 38 | + // The 4 is the amount of bytes per pixel which is always 4 for the DX11ScreenCapture |
| 39 | + int stride = fullscreen.CaptureWidth * 4; |
| 40 | + |
| 41 | + Span<byte> data = new(fullscreen.Buffer); |
| 42 | + |
| 43 | + // Iterate all rows of the image |
| 44 | + for (int y = 0; y < fullscreen.Height; y++) |
| 45 | + { |
| 46 | + // Select the actual data of the row |
| 47 | + Span<byte> row = data.Slice(y * stride, fullscreen.Width * 4); |
| 48 | + |
| 49 | + // Iterate all pixels |
| 50 | + for (int x = 0; x < fullscreen.Width; x += 4) |
| 51 | + { |
| 52 | + // Data is in BGRA format for the DX11ScreenCapture |
| 53 | + byte b = row[x]; |
| 54 | + byte g = row[x + 1]; |
| 55 | + byte r = row[x + 2]; |
| 56 | + byte a = row[x + 3]; |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | +``` |
0 commit comments