3

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?

ghoul
  • 890
  • 1
  • 10
  • 17
  • If the screen your're trying to take a picture of is set to scale and your application is not DPIAware, you will receive *virtualized* measures (scaled to a predefined DPI value). First, try setting your app's DPI Awareness to at least System Aware. See here: [How to configure an app to run correctly on a machine with a high DPI setting](https://stackoverflow.com/a/13228495/7444103) how to *activate* it. [See here](https://stackoverflow.com/a/50276714/7444103) for further details on specialized API functions. – Jimi Jan 14 '19 at 02:44

4 Answers4

5

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) {
    }
 }
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
2

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.

mr.coffee
  • 962
  • 8
  • 22
0

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);
0
// Capture the screen
Bitmap screenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics graphics = Graphics.FromImage(screenshot);
graphics.CopyFromScreen(0, 0, 0, 0, screenshot.Size);

// Display the screenshot on the picture box
pictureBox1.Image = screenshot;
Akram Al-Qaifi
  • 386
  • 1
  • 11