C# Screenshot Full Window

Viewed 4886

I'm trying to write a console app using .NET Framework. I want to screenshot my screen. I've used other answers on SO like this:

https://stackoverflow.com/a/24879511/9457997

The issue is that this doesn't capture my whole screen. It's missing about 1/5 on the bottom and the right side.

How can I capture my whole screen using C# .NET Framework?

3 Answers

Use VirtualScreen:

int screenLeft = SystemInformation.VirtualScreen.Left;
int screenTop = SystemInformation.VirtualScreen.Top;
int screenWidth = SystemInformation.VirtualScreen.Width;
int screenHeight = SystemInformation.VirtualScreen.Height;

// Create a bitmap of the appropriate size to receive the full-screen screenshot.
using (Bitmap bitmap = new Bitmap(screenWidth, screenHeight))
{
    // Draw the screenshot into our bitmap.
    using (Graphics g = Graphics.FromImage(bitmap))
    {
        g.CopyFromScreen(screenLeft, screenTop, 0, 0, bitmap.Size);
    }

    //Save the screenshot as a Jpg image
    var uniqueFileName = "C:\\temp\\a.Jpg";
    try
    {
        bitmap.Save(uniqueFileName, ImageFormat.Jpeg);
    }
    catch (Exception ex) {
    }
 }

You can use Screen.PrimaryScreen.Bounds; to get the bounds of your screen.

Rectangle bounds = Screen.PrimaryScreen.Bounds;
using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
using (Graphics g = Graphics.FromImage(bitmap))
{
    g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
    bitmap.Save("C://test.jpg", ImageFormat.Jpeg);
}

You will need to reference System.Drawing, System.Drawing.Imaging and System.Windows.Forms for this code sample to work in a Console application.

I have unfortunately not found a way to screenshot from a console application, but here is my way to screenshot and let the user pick where to save the pic by using SaveFileDialog.

For the following code, you will need these three references:

using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

You can attach this code to a button or an event:

Bitmap bt = new Bitmap(Screen.PrimaryScreen.Bounds.Width, 
Screen.PrimaryScreen.Bounds.Height);
Graphics g = Graphics.FromImage(bt);
g.CopyFromScreen(0, 0, 0, 0, bt.Size);
SaveFileDialog sfd = new SaveFileDialog();
sfd.ShowDialog();
string name = sfd.FileName + ".jpg";
bt.Save(name, ImageFormat.Jpeg);
Related