首页 > 解决方案 > 无法捕获全屏图像??(带有任务栏和任何其他打开的窗口)

问题描述

我对这段代码有疑问:

我需要截取我看到的所有东西(任务栏,任何打开的东西)的全屏。

我的代码只是给了我一个窗口的裁剪图片(像这张照片)

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

标签: c#winforms

解决方案


您的显示设置设置为 125%(或更高)缩放。

您的应用程序不支持 DPI。您可以通过更新应用程序的 manifest来纠正它。

如果这对您不起作用(或者您不想使用清单),您可以 pinvoke GetDeviceCapsAPI 以获得正确的宽度和高度CopyFromScreen

以下是您的本地定义:

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

你会这样称呼它:

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

推荐阅读