Recursive AI

Capture the screen in C#

OnePlatform is a free, open-source library for windows, input, clipboard, displays and gamepads in .NET from Recursive AI. MIT or Apache-2.0, no native DLLs to ship.

Save the primary display to screen.png. On Linux with Wayland, this captures the whole desktop.

Terminal
dotnet add package RecursiveAI.OnePlatform

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using System.IO;
using OnePlatform.SystemDesktop;
 
var display = ScreenCapture.GetDisplays()[0];
var shot = await ScreenCapture
    .CaptureDisplayAsync(display.Id);
File.WriteAllBytes("screen.png", shot.ToPng());
System.Console.WriteLine("Saved screen.png");

Paste the code above into Program.cs of a new dotnet new console project and dotnet run, or unzip the example and dotnet run.

Run this from your graphical desktop session. No input files are needed. The example writes screen.png in the current directory and prints:

Text
Saved screen.png

Open screen.png to see what was visible when the capture ran. The example replaces any existing file with that name. If permission is missing or you decline the capture, OnePlatform throws OsPermissionException before the file is written.

Variations

To use an X11 display explicitly, for example on an X11 test server, replace the two capture statements with these. The X11 backend reads the DISPLAY environment variable by default.

C#
var settings = new ScreenCaptureOptions
{
    Backend = ScreenCaptureBackend.X11
};
var displays = ScreenCapture.GetDisplays(settings);
var shot = await ScreenCapture
    .CaptureDisplayAsync(displays[0].Id, settings);

The short answer

Call ScreenCapture.GetDisplays(), pass a display’s Id to CaptureDisplayAsync, and write the result of ToPng() to disk. You don’t need to open a OnePlatform window or run its event loop. Use the async call in a desktop app so you don’t wait synchronously for a Wayland portal reply.

How do you take a screenshot of the whole screen in C#?

On Windows, macOS, and X11, the example captures one complete display, including whichever windows are visible on it at the time. The display list is primary first, so [0] selects the primary display. To let the user choose another monitor, show the entries’ Name and Bounds and pass the selected entry’s Id back to the capture call. The list index isn’t the display ID.

Bounds describes where a monitor sits on the desktop. On macOS those coordinates are in points, not image pixels; Scale tells you how many pixels correspond to one bounds unit. If you need the PNG’s actual dimensions, read Width and Height on the returned ScreenshotImage. On Wayland, the screenshot portal exposes a single desktop entry with unknown bounds, so use the returned image dimensions there too.

Can you take a screenshot of just one window in C#?

Not with ScreenCapture. It takes a display ID, not a window handle, and reads what’s visible on the display. Cropping that image afterward can isolate a visible rectangle, but it won’t recover a window’s hidden contents or leave out a different window that overlaps it.

Does screen capture need permission on macOS or Linux?

On macOS, give the app Screen Recording permission in System Settings. OnePlatform checks permission before capturing rather than returning a screenshot of only the wallpaper. On Linux with X11, capture reads the X display directly; if you run that backend under Xwayland, it sees X windows only. On native Wayland, the desktop’s XDG Screenshot portal handles the request and may ask the user to approve it. A missing service is reported as ShellUnavailableException; a canceled portal request reports OsPermissionException. On Windows, run from an interactive desktop session.

All oneplatform recipes