Taking screenshots (screen captures) of user displays in a .NET application is quite straightforward on Windows thanks to the Graphics.CopyFromScreen() method. If you want to take screenshots in your .NET Mono application on Linux or Mac OS X however, you will experience difficulties as System.Drawing implementation is incomplete on Mono.
Another limitation of Graphics.CopyFromScreen() is that you cannot take a screenshot of multiple displays at once.
We have stumbled on these problems when we needed to take screen captures reliably on all three platforms in our time tracking application Screenshot Monitor. So we have created a tiny open source .NET library that allows receiving screenshots of the main or all computer displays under Windows, Linux or Mac OS X (with Mono).
You can add it to your project via NuGet package:
PM> Install-Package Pranas.ScreenshotCapture
Then, you can start taking screenshots like this:
// take screenshot from primary display only Image screen = Pranas.ScreenshotCapture.TakeScreenshot(true); screen.Save("PrimaryDisplay.png");
This library also allows taking screenshots from all of the displays at once with a single call like this:
// take screenshot from all displays at once Image screen = Pranas.ScreenshotCapture.TakeScreenshot(); screen.Save("AllDisplays.png");
You can grab the source code or fork us on GitHub. The library is under MIT license, so basically can be used anywhere. Happy coding!
5 thoughts on “Capturing screenshots in .NET and Mono”
Works well, but there is a general UI freeze.
Incompatible for projects (as mine) which need to analyze screen 10-20 times per second.
I know this is a super old comment, but now you have me curious. What in the world would you be capturing on the screen 10-20 times per second other than video? I am fairly certain that isn’t what that API is meant to be used for anyways. You really can’t use .Net for anything like that unless it is just a wrapper from another library.
How can I capture just secondary screen
The tool doesn’t allow doing that. However you can review the implementation there:
So you can alter it to capture the screen you need.
Well, there are cases in which you need. I work on cameras and need to make sure the camera app preview of a new product we are launching in Android has no artifacts over long period of time. It is either this and check the screen shot or me sitting for 12 hours watching at the camera doing preview.