带缩放功能的 Windows 屏幕截图

问题描述 投票:0回答:3

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

  • 我的主显示器的“比例和布局”设置为 200%。
  • 我的中学 将“比例和布局”设置为 100%。

我正在尝试使用 C# 来截取每个监视器的屏幕截图。 但是我的缩放显示器报告了错误的坐标: 一切都减半了。 结果,主显示器的屏幕截图仅为屏幕面积的 1/4(宽度/2,高度/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,那么辅助监视器的边界就会加倍(在屏幕截图中留下大的黑色区域)。

c# .net windows gdi
3个回答
4
投票

正如评论中提到的,解决方案是将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>

0
投票

是的,添加清单是正确的方法。我尝试了所有解决方案,最终将 app.manifest 添加到我的项目中,内容如下:

<?xml version="1.0" encoding="utf-8"?>

<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">

  <application xmlns="urn:schemas-microsoft-com:asm.v3">
    <windowsSettings>
      <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">True/PM</dpiAware>
      <dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2, PerMonitor</dpiAwareness>
    </windowsSettings>
  </application>

</assembly>

终于不需要导入任何 ddl 或其他什么了。


-1
投票

您可以尝试以下操作:如何获取 Windows 显示设置?

这种方法对我有用

© www.soinside.com 2019 - 2024. All rights reserved.