Graphics.CopyFromScreen copies a rectangle of the desktop into a Bitmap. That is all you need to take a screenshot of one monitor, all of them, or any region you can describe with screen coordinates.
The code uses System.Drawing and WinForms’ Screen class, so it runs on Windows only. In a console project, set <TargetFramework>net8.0-windows</TargetFramework> and <UseWindowsForms>true</UseWindowsForms> in the .csproj; a WinForms project already has both.
Capture the primary screen
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
Application.SetHighDpiMode(HighDpiMode.PerMonitorV2);
using Bitmap shot = CaptureScreen(Screen.PrimaryScreen!.Bounds);
shot.Save("screenshot.png", ImageFormat.Png);
static Bitmap CaptureScreen(Rectangle area)
{
var bitmap = new Bitmap(area.Width, area.Height, PixelFormat.Format32bppArgb);
using Graphics g = Graphics.FromImage(bitmap);
g.CopyFromScreen(area.Location, Point.Empty, area.Size, CopyPixelOperation.SourceCopy);
return bitmap;
}
CaptureScreen takes a rectangle in screen coordinates and returns a bitmap of exactly that size. The caller owns the bitmap and must dispose it; a full-HD screenshot holds about 8 MB of unmanaged memory that the garbage collector does not see.
Save screenshots as PNG. JPEG compression smears text and thin lines, which is usually what a screenshot is for.
All monitors, or one at a time
On a multi-monitor setup, SystemInformation.VirtualScreen is the rectangle that covers every screen. Its X and Y can be negative when a monitor sits to the left of or above the primary one, and CopyFromScreen handles that correctly:
using Bitmap desktop = CaptureScreen(SystemInformation.VirtualScreen);
desktop.Save("all-monitors.png", ImageFormat.Png);
To get one file per monitor, loop over Screen.AllScreens:
Screen[] screens = Screen.AllScreens;
for (int i = 0; i < screens.Length; i++)
{
using Bitmap shot = CaptureScreen(screens[i].Bounds);
shot.Save($"monitor-{i + 1}.png", ImageFormat.Png);
}
Areas where no monitor exists (for example, the empty corner when two screens of different heights sit side by side) come out black in the virtual-screen capture.
The DPI trap
If Windows display scaling is set above 100% and your process is not DPI aware, Windows lies to it: Screen.Bounds reports scaled-down sizes, and the screenshot covers only the top-left part of the screen. That is why the example calls Application.SetHighDpiMode(HighDpiMode.PerMonitorV2) before touching any screen API.
In a WinForms app created from the .NET 6 or later template, ApplicationConfiguration.Initialize() already sets a DPI mode from the project’s ApplicationHighDpiMode property. Set it to PerMonitorV2 in the .csproj if you capture screens on mixed-DPI setups. The DPI mode can only be set once per process and must be set before the first window is created.
Capture your own form
For a window in your own application, you do not need the screen at all. Control.DrawToBitmap asks the form to paint itself into a bitmap, so it works even when the form is partly covered by another window:
using var bitmap = new Bitmap(form.Width, form.Height);
form.DrawToBitmap(bitmap, new Rectangle(Point.Empty, form.Size));
bitmap.Save("form.png", ImageFormat.Png);
DrawToBitmap does not handle every control well. Controls that render through other means (a WebBrowser, a RichTextBox, some third-party controls) may come out blank. In those cases, capture the form’s screen rectangle with CaptureScreen(form.Bounds) while it is on top.
What about BitBlt?
Older examples, including the first version of this post, call GetDC, BitBlt and ReleaseDC from user32.dll and gdi32.dll through P/Invoke. CopyFromScreen does the same BitBlt call internally and releases the device contexts for you, so there is no reason to write that code yourself. The P/Invoke route only makes sense if you need PrintWindow to capture a window that is hidden behind others, or you want to avoid the WinForms dependency entirely.
Neither approach captures content that the application protects with SetWindowDisplayAffinity (some video players and password managers); those areas come out black. For recording or continuous capture, the Windows.Graphics.Capture API is the modern option, at the cost of more setup.
There is no cross-platform screenshot API in .NET. Libraries such as ImageSharp and SkiaSharp can process the image once you have it, but capturing the screen on macOS or Linux requires each platform’s own APIs.
I have to thank you for the efforts you have put in writing this website.
I’m hoping to see the same high-grade blog posts from you in the
future as well. In truth, your creative writing abilities has inspired me to get my
own, personal blog now 😉
This is my first time pay a quick visit at here and i am
truly pleassant to read everthing at one place.