Screen Capture
Bitmap Conversion
Desktop Screenshot
Image Processing
Programming Tutorial

Capture the Screen into a Bitmap

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Capturing the screen into a bitmap means copying pixels from the desktop framebuffer into an in-memory image object. On Windows, the simplest high-level approach in .NET is to create a Bitmap and fill it with Graphics.CopyFromScreen.

What “Bitmap” Means Here

In desktop programming, a bitmap is just a pixel buffer with width, height, and color data. Once the screen is copied into a bitmap, you can save it, crop it, inspect pixels, or send it to another part of the application.

That makes screen capture useful for:

  • screenshot tools
  • automated testing
  • remote support utilities
  • visual debugging

If you are writing C# on Windows, System.Drawing.Bitmap is the most direct representation for this task.

Basic C# Example

The following example captures the primary screen and saves it as a PNG. It is small enough to run in a WinForms project or another Windows desktop context.

csharp
1using System.Drawing;
2using System.Drawing.Imaging;
3using System.Windows.Forms;
4
5class Program
6{
7    static void Main()
8    {
9        Rectangle bounds = Screen.PrimaryScreen.Bounds;
10
11        using Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height);
12        using Graphics graphics = Graphics.FromImage(bitmap);
13
14        graphics.CopyFromScreen(bounds.Location, Point.Empty, bounds.Size);
15        bitmap.Save("screenshot.png", ImageFormat.Png);
16    }
17}

The workflow is simple:

  1. get the screen bounds
  2. allocate a bitmap of the same size
  3. copy pixels from the screen into that bitmap
  4. save or process the image

Capturing a Specific Region

You do not always need the full display. A region capture is often faster and produces smaller images.

csharp
1using System.Drawing;
2using System.Drawing.Imaging;
3
4class RegionCapture
5{
6    static void Main()
7    {
8        Rectangle region = new Rectangle(100, 100, 400, 300);
9
10        using Bitmap bitmap = new Bitmap(region.Width, region.Height);
11        using Graphics graphics = Graphics.FromImage(bitmap);
12
13        graphics.CopyFromScreen(region.Location, Point.Empty, region.Size);
14        bitmap.Save("region.png", ImageFormat.Png);
15    }
16}

This is the same technique, but the source rectangle is smaller.

Multi-Monitor Considerations

On systems with more than one display, capturing only PrimaryScreen may miss other monitors. In WinForms, SystemInformation.VirtualScreen gives the full desktop rectangle across all monitors.

csharp
1using System.Drawing;
2using System.Drawing.Imaging;
3using System.Windows.Forms;
4
5class AllScreensCapture
6{
7    static void Main()
8    {
9        Rectangle bounds = SystemInformation.VirtualScreen;
10
11        using Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height);
12        using Graphics graphics = Graphics.FromImage(bitmap);
13
14        graphics.CopyFromScreen(bounds.Location, Point.Empty, bounds.Size);
15        bitmap.Save("all-screens.png", ImageFormat.Png);
16    }
17}

That is usually what you want for support tools or full-session capture.

Lower-Level Windows API Context

Under the hood, screen capture on Windows is often described in terms of GDI calls such as GetDC, CreateCompatibleBitmap, and BitBlt. Those APIs are still useful when you need tight control or are writing C or C++, but for many .NET applications Graphics.CopyFromScreen is a cleaner wrapper around the same basic idea.

If you later need advanced behavior such as capturing hidden windows, minimizing DPI issues, or handling accelerated surfaces, you may need to move down to native APIs or newer capture APIs. For straightforward screenshots, the bitmap approach remains fine.

Common Pitfalls

One problem is trying to use System.Drawing in an unsupported environment. The pattern above is intended for Windows desktop applications. It is not a general-purpose server-side screenshot technique.

Another issue is DPI scaling. The pixels you capture may not line up with logical UI coordinates if your application is not DPI-aware. Test on machines with scaling enabled, especially at 125% or 150%.

A third pitfall is capturing too early. If the target window has not finished painting, the bitmap can contain stale or partial pixels. When you depend on timing, add a reliable wait or capture after the UI event that confirms rendering is complete.

Summary

  • To capture the screen into a bitmap in .NET, create a Bitmap and call Graphics.CopyFromScreen.
  • Use Screen.PrimaryScreen.Bounds for one monitor or SystemInformation.VirtualScreen for all monitors.
  • Region capture works the same way with a smaller rectangle.
  • 'Bitmap gives you an in-memory image you can save or inspect.'
  • Watch for Windows-only constraints, DPI scaling, and timing issues.

Course illustration
Course illustration

All Rights Reserved.