How to Capture Desktop and Active Window Screenshot in C#

  1. Capture Desktop Screenshot in C#
  2. Capture Screenshot of Active Window in C#
How to Capture Desktop and Active Window Screenshot in C#

The .Net Framework can capture the screenshots in a few lines of code without using any scripting language. It makes the complete C# code understandable for developers and easy to modify.

This tutorial describes how to capture a single image of the display or active window in C#.

In Windows 10, the Windows.Graphics.Capture is a powerful namespace that offers different APIs to capture screenshots. Desktop and active window screenshots invoke developers to make secure system user interfaces for end-users.

Capture Desktop Screenshot in C#

In Visual Studio, create a C# application and name it Screenshot. Afterward, go to the toolbox and add a button1 control to the Form1.cs [Design], and the following code will now be executable in Form1.cs.

// add the following directories
using System;
using System.Drawing;
using System.Drawing.Imaging;

namespace Screenshot {
  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e) {}

    // create a clieck event for the `Screenshot` button
    private void button1_Click(object sender, EventArgs e) {
      Screenshot();
    }
    private void Screenshot() {
      try {
        // set the screenshot resolution based on your monitor's resolution
        Bitmap captureBitmap = new Bitmap(1024, 768, PixelFormat.Format32bppArgb);
        Rectangle captureRectangle = Screen.AllScreens[0].Bounds;
        Graphics captureGraphics = Graphics.FromImage(captureBitmap);
        captureGraphics.CopyFromScreen(captureRectangle.Left, captureRectangle.Top, 0, 0,
                                       captureRectangle.Size);

        // select the save location of the captured screenshot
        captureBitmap.Save(@"E:\Capture.jpg", ImageFormat.Jpeg);

        // show a message to let the user know that a screenshot has been captured
        MessageBox.Show("Screenshot taken! Press `OK` to continue...");
      }

      catch (Exception ex) {
        MessageBox.Show("Error! " + ex.Message);
      }
    }
  }
}

The first step of implementing a Windows Form Application is to add the screen capture capability to your C# project. Open Package.appxmaanifest in the Solution Explorer and select the Capabilities tab to check the Graphics Capture.

Before launching the system UI, it’s important to check if your C# application is capable of taking a screenshot. The device not meeting the hardware requirements or the system not giving access to your application can be the possible reasons for your C# application’s disability to take screenshots.

Use the IsSupported method in the GraphicsCaptureSession class to determine if your C# application can capture desktop or active window screenshots.

public void OnInitialization() {
  // check if the C# application supports screen capture
  if (!GraphicsCaptureSession.IsSupported()) {
    // this will hide the visibility of the `Screenshot` button if your C# application does not
    // support the screenshot feature
    button1.Visibility = Visibility.Collapsed;
  }
}

It’s possible to capture a screenshot of the desktop containing all open windows using the CopyFromScreen() method from the Graphics object in C#.

using System;
using System.Drawing;
using System.Drawing.Imaging;

namespace Screenshot {
  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e) {}

    private void button1_Click(object sender, EventArgs e) {
      CaptureMyScreen();
    }
    private void CaptureMyScreen() {
      Rectangle rect = Screen.GetBounds(Point.Empty);
      using (Bitmap bitmap = new Bitmap(rect.Width, rect.Height)) {
        using (Graphics g = Graphics.FromImage(bitmap)) {
          g.CopyFromScreen(Point.Empty, Point.Empty, rect.Size);
        }
        // select the save location of the captured screenshot
        bitmap.Save(@"E://Capture.png", ImageFormat.Png);

        // show a message to let the user know that a screenshot has been captured
        MessageBox.Show("Screenshot taken! Press `OK` to continue...");
      }
    }
  }
}

Capture Screenshot of Active Window in C#

When multiple applications running lead to many windows showing up, it’s important to only capture the active window. An active window is the one that currently holds the keyboard focus.

While the CopyFromScreen() method from the Graphics object in C# can capture a screenshot of the desktop capturing all the open windows, using this.Bounds gets the job done.

using System;
using System.Drawing;
using System.Drawing.Imaging;

namespace Screenshot {
  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e) {}

    private void button1_Click(object sender, EventArgs e) {
      CaptureMyScreen();
    }
    private void CaptureMyScreen() {
      Rectangle rect = this.Bounds;
      using (Bitmap bitmap = new Bitmap(rect.Width, rect.Height)) {
        using (Graphics g = Graphics.FromImage(bitmap)) {
          g.CopyFromScreen(new Point(rect.Left, rect.Top), Point.Empty, rect.Size);
        }
        // select the save location of the captured screenshot
        bitmap.Save(@"E://Capture.png", ImageFormat.Png);

        // show a message to let the user know that a screenshot has been captured
        MessageBox.Show("Screenshot taken! Press `OK` to continue...");
      }
    }
  }
}

This tutorial covered three ways to capture desktop screenshots, all open and active windows in C#. It’s easy to customize the above C# code according to your taste.

However, it’s the most optimized C# code for capturing screenshots.

Syed Hassan Sabeeh Kazmi avatar Syed Hassan Sabeeh Kazmi avatar

Hassan is a Software Engineer with a well-developed set of programming skills. He uses his knowledge and writing capabilities to produce interesting-to-read technical articles.

GitHub