this post was submitted on 14 Apr 2025
9 points (100.0% liked)

.NET

1655 readers
9 users here now

Getting started

Useful resources

IDEs and code editors

Tools

Rules

Related communities

Wikipedia pages

founded 2 years ago
MODERATORS
 

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.

top 12 comments
sorted by: hot top controversial new old
[–] [email protected] 6 points 1 week ago (1 children)

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"    "{}"

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:

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

[–] [email protected] 2 points 1 week ago

Thank you so much for the detailed reply!

[–] TwilightKiddy 3 points 1 week ago (1 children)

Maybe you can try looking through KDE's Spectale source code to see how they do it?

[–] [email protected] 2 points 1 week ago

Nice idea. I've had a look and it appears to be embedded in a lot of Qt code that is hard for me to follow and/or use in dotnet :/

[–] Kissaki 2 points 1 week ago (2 children)

Sorry this will be all abstract. By now I feel like it's just useless rambling. I guess I'll post what I wrote anyway. Just in case it is somewhat helpful or contextualizing.

MAUI is the cross platform dotnet framework/library, although without Linux support. But as I remember there is a community project to extend it onto Linux. That's where I would check first.

Other than that, I assume you have looked for other libs. But I am not very hopefuly they exist to cross Wayland and X11.

I would be more hopeful that there are separate dotnet libs for wayland and X11. But who knows what state those would be in.


of a standard library

You mean the X11 and Wayland "standard" libraries? Or dotnet?

Notably, you are talking about GUI functionality here. Most programming language standard libraries cover everything except for GUI. Because GUI is a whole mess in and of itself - as you found yourself.

Linux has not just one but multiple graphics desktop standards. So it's not as simple as on Windows with one Windows API or macOS with one system. That's where the whole ordeal comes from of not quite being able to support one platform and system, and them being different.

[–] [email protected] 2 points 1 week ago (1 children)

It seems this function is missing in MAUI on linux yet :/

To be fair, I think there are almost more GUI frameworks specifically for dotnet on Windows, each of which has been "the standard that everyone should follow" for a year or two, than there are DEs for linux. It's ridiculous how often Microsoft changes their mind.

[–] Kissaki 1 points 1 week ago (1 children)

It’s ridiculous how often Microsoft changes their mind.

I'm just glad they still support Windows Forms through all of that. Which is kinda insane through all that. Microsoft holding long term support and maintenance very high.

There's some connecting matter between the technologies as well. Like the XML representation between WPF and MAUI and whatever else there is. Or Razor between ASP.NET MVC, pages, and Blazor. WinUI and Uno plattform are part of MAUI I think? It ends up very confusing, and yes it's numerous technologies. But there are some cross-sections at least.

I'm familiar with Windows Forms, WPF, and Blazor. I disliked the XML UI representation. I've always wanted to look into/prototype the code-style declaration of UI in code rather than XML.

GUI frameworks are always a hassle. Maybe the number of frameworks and diversity is proof that there is inherent complexity that's hard to solve well.

[–] SmartmanApps 1 points 1 week ago

I’ve always wanted to look into/prototype the code-style declaration of UI in code rather than XML

Creating MAUI UI's in C#

[–] LeFantome 0 points 1 week ago (1 children)

Linux is not a supported target for MAUI apps.

[–] Kissaki 2 points 1 week ago

Yes, that's what I wrote.

[–] LeFantome 2 points 1 week ago (1 children)

I think ffmpeg is overkill. I would use ImageMagick or Scrot. That said, it makes sense to call a utility to do it.

If you are using Uno Platform, there is a TakeScreenshot class. I have not used it. Uno Platform targets Linux so that may work.

There is a Screenshot class in MAUI but MAUI does not target Linux. You could try the unofficial port but I have no experience with it: https://github.com/jsuarezruiz/maui-linux

[–] [email protected] 1 points 1 week ago* (last edited 1 week ago)

scrot does not seems to work on wayland, but it looks very nice for X11. I've also found Flameshot, which claims to work on KDE wayland and, albeit with a forced confirmation dialog, on gnome wayland. I'll update my post if I tested these tools more thoroughly.