Skip to content

Instantly share code, notes, and snippets.

@floko84
Created July 27, 2021 21:37
Show Gist options
  • Save floko84/97f86d862176eda1ae2bde020cfd3536 to your computer and use it in GitHub Desktop.
Save floko84/97f86d862176eda1ae2bde020cfd3536 to your computer and use it in GitHub Desktop.
Capture a screenshot of the entire desktop in C#
using System;
using System.Drawing;
using System.Windows.Forms;
public static class ScreenCapture
{
/// <summary>
/// Creates an <see cref="Image"/> object containing a screen shot of the entire desktop.
/// </summary>
/// <returns></returns>
public static Image CaptureDesktop()
{
var bounds = SystemInformation.VirtualScreen;
var bitmap = new Bitmap(bounds.Width, bounds.Height);
try
{
using (var g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(bounds.Location, Point.Empty, bounds.Size);
}
return bitmap;
}
catch
{
bitmap.Dispose();
throw;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment