Taking a screenshot in Wayland is tricky, even more so in C#. I'm not aware of a up to date library that takes care of these things for you on Linux in C#.
Your best bet for a clean solution is most likely using the org.freedesktop.portal.Desktop
portal via d-bus.
There's a d-bus library for C#: https://github.com/tmds/Tmds.DBus
Essentially you want to call org.freedesktop.portal.Screenshot.Screenshot
on org.freedesktop.portal.Desktop
and then get the file path for the screenshot from the object path that gets returned.
Here's a quick example to take a screenshot from the terminal:
gdbus call --session --dest org.freedesktop.portal.Desktop --object-path /org/freedesktop/portal/desktop --method org.freedesktop.portal.Screenshot.Screenshot ":1.0" "{}"
Other solutions that work via Wayland are:
- Getting a screen capture via Pipewire
- Call window manager specific d-bus APIs (e.g.
org.kde.KWin.ScreenShot2
)
You need to test with different desktop environments though, the amount of user interaction required to take a screenshot on Wayland varies.