Windows 带缩放的屏幕截图

Windows screenshot with scaling

我正在尝试截取我 windows 10 机器上每个屏幕的屏幕截图。 我的系统有 2 个多 DPI 显示器。

我正在尝试使用 C# 截取每个监视器的屏幕截图。 但是我的缩放显示器报告了错误的坐标: 一切都减半。 结果,主显示器的屏幕截图只有屏幕区域的 1/4 (width/2,height/2)。

这是我的代码:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;

namespace CsSourceClient
{
    class Program
    {
        static void Main(string[] args)
        {
            Screen[] screens;
            screens = Screen.AllScreens;


            int i = 0;

            foreach (var screen in System.Windows.Forms.Screen.AllScreens)
            {
                var savePath = "monitor" + i+".jpg";
                var x = screen.Bounds.Location.X;
                var y = screen.Bounds.Location.Y;
                var w = screen.Bounds.Width;
                var h = screen.Bounds.Height;
                Console.Out.WriteLine("Monitor: " + i + " extents:" + x + "," + y + " " + w + "," + h);
                using (Bitmap bmp = new Bitmap(w, h))
                {
                    using (Graphics g = Graphics.FromImage(bmp))
                    {
                        g.CopyFromScreen(x, y, 0, 0, bmp.Size);
                    }

                    bmp.Save(savePath, ImageFormat.Jpeg);
                    Console.Out.WriteLine("saved: " + savePath);
                }
                i++;
            }



        }
    }
}

PS:我根据我在清单文件中设置的 "dpiaware" 得到不同的值:https://social.msdn.microsoft.com/Forums/en-US/054c1d49-9f24-4617-aa83-9bc4f1bb4a5d/use-appmanifest-file-to-make-winforms-application-dpi-aware?forum=vbgeneral

如果我不设置 dpiaware,则主监视器的边界会减半(裁剪出屏幕的 3/4)。 如果我将 dpiaware 设置为 true,则辅助监视器的边界会加倍(在屏幕截图中留下大的黑色区域)。

如评论中所述,解决方案是将app.manifest dpiAware 标签设置为"true/pm"。效果与仅将其设置为 true 不同。

<application xmlns="urn:schemas-microsoft-com:asm.v3">
    <windowsSettings>
      <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true/pm</dpiAware>
    </windowsSettings>
  </application>

您可以尝试类似的操作:How to get Windows Display settings?

这种方法对我有用