2

i have a problem with this code :

i need to screenshot the full screen that i see with all things (taskbar, anything open).

my code is just giving me a cropped pic of just one window (like this pic)

Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, 
    Screen.PrimaryScreen.Bounds.Height);
Graphics graphics = Graphics.FromImage(bitmap as Image);

graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
bitmap.Save("D://Changes.jpg", ImageFormat.Jpeg);
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86

1 Answers1

2

Your display settings are set to 125% (or higher) zoom.

Your application isn't DPI aware. You can correct that by updating your application's manifest.

If that doesn't work for you (or you'd rather not use the manifest), you can pinvoke GetDeviceCaps API to get the correct width and height for CopyFromScreen.

Here are your native definitions:

private static class Win32Native
{
    public const int DESKTOPVERTRES = 0x75;
    public const int DESKTOPHORZRES = 0x76;

    [DllImport("gdi32.dll")]
    public static extern int GetDeviceCaps(IntPtr hDC, int index);
}

And you'd call it as so:

int width, height;
using(var g = Graphics.FromHwnd(IntPtr.Zero))
{
    var hDC = g.GetHdc();
    width = Win32Native.GetDeviceCaps(hDC, Win32Native.DESKTOPHORZRES);
    height = Win32Native.GetDeviceCaps(hDC, Win32Native.DESKTOPVERTRES);
    g.ReleaseHdc(hDC);
}

using (var img = new Bitmap(width, height))
{
    using (var g = Graphics.FromImage(img))
    {
        g.CopyFromScreen(0, 0, 0, 0, img.Size);
    }
    img.Save(@"C:\users\andy\desktop\test.jpg", ImageFormat.Jpeg);
}
Andy
  • 12,859
  • 5
  • 41
  • 56