---
title: "Capture the screen in C#"
url: https://recursiveai.org/oneplatform/cookbook/capture-the-screen-in-csharp/
description: "Capture the screen in C# and save it as a PNG with OnePlatform. Pick a display and see what permissions Windows, macOS, X11, and Wayland need."
version: "1.2.0"
---

# 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.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OnePlatform) · [GitHub](https://github.com/recursive-ai-oss/oneplatform-csharp) · [Docs](https://recursiveai.org/oneplatform/docs/) · [Download example (.zip)](https://recursiveai.org/oneplatform/cookbook/capture-the-screen-in-csharp/capture-the-screen-in-csharp.zip)

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

```shell
dotnet add package RecursiveAI.OnePlatform
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/oneplatform/cookbook/capture-the-screen-in-csharp/capture-the-screen-in-csharp.zip) 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.

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

## Related recipes

- [Send system-wide keyboard and mouse input from C#](https://recursiveai.org/oneplatform/cookbook/send-keyboard-and-mouse-input-to-other-apps-csharp/)
- [Handle per-monitor DPI](https://recursiveai.org/oneplatform/cookbook/handle-per-monitor-dpi/)

## 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.
