Created
July 27, 2021 21:37
-
-
Save floko84/97f86d862176eda1ae2bde020cfd3536 to your computer and use it in GitHub Desktop.
Capture a screenshot of the entire desktop in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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