简体   繁体   中英

C# Screenshot Full Window

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?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM