From 8daae657453afa99f3d10fc9cb6e36503f6271b9 Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Tue, 25 May 2021 00:48:15 +0200 Subject: [PATCH] Improved readme --- README.md | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/README.md b/README.md index a5ff4ce..b35d9d3 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,60 @@ # ScreenCapture Vortice based Desktop Duplication + +## Usage +```csharp +// Sets the DPI-awareness of the application - this is required for capturing +DPIAwareness.Initalize(); + +// 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 om the screen +// Capture the whole screen +CaptureZone 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 +CaptureZone topLeft = screenCapture.RegisterCaptureZone(0, 0, 100, 100, downscaleLevel: 1); + +// Capture the screen +// This should be done in a loop on a seperate 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) +// Locking is not neccessary in that case as we're capturing in the same thread, +// but when using a threaded-approach (which is recommended) it prevents potential tearing of the data in the buffer +lock (fullscreen.Buffer) +{ + // Since the size of the capture can be bigger than the size of our captured region due to size constraints on the GPU, + // we need to use this for the stride. + // The 4 is the amount of bytes per pixel which is always 4 for the DX11ScreenCapture + int stride = fullscreen.CaptureWidth * 4; + + Span data = new(fullscreen.Buffer); + + // Iterate all rows of the image + for (int y = 0; y < fullscreen.Height; y++) + { + // Select the actual data of the row + Span row = data.Slice(y * stride, fullscreen.Width * 4); + + // Iterate all pixels + for (int x = 0; x < fullscreen.Width; x += 4) + { + // Data is in BGRA format for the DX11ScreenCapture + byte b = row[x]; + byte g = row[x + 1]; + byte r = row[x + 2]; + byte a = row[x + 3]; + } + } +} +``` \ No newline at end of file