I want to take a screenshot. In Windows, that’s a simple Graphics::CopyFromScreen call.
In Linux, I feel a little confused on how to do this. It seems there is a principal and stark distinction between X11 and Wayland, so I have to include both code paths. For either, it seems there is quite a lot of boilerplate code, often tagged as ‘may break depending on your configuration, good luck’.
Effectively, what I found is recommended most often is to call ffmpeg to let it handle that. I’m sure that works, but I find it rather unpalatable.
I find this strange. Taking a screenshot is, in my mind at least, supposed to be a straightforward part of a standard library. Perhaps it is, and I just completely missed it? If not, is there a good library that works out-of-the-box on most variants of linux?
Update: Thank you all for the input. I eventually went with calling ImageMagick. It is fast, easy to use, well documented, and supports capturing arbitrary displays with little effort.
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
onorg.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" "{}"
https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.impl.portal.Screenshot.html#org-freedesktop-impl-portal-screenshot-screenshot
Other solutions that work via Wayland are:
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.
Thank you so much for the detailed reply!