Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 具有缩放功能的Windows屏幕截图_C#_.net_Windows_Gdi - Fatal编程技术网

C# 具有缩放功能的Windows屏幕截图

C# 具有缩放功能的Windows屏幕截图,c#,.net,windows,gdi,C#,.net,Windows,Gdi,我正在尝试在我的Windows10机器上拍摄每个屏幕的截图。 我的系统有两个监视器,它们具有多个DPI 我的主显示器的“比例和布局”设置为200% 我的中学 将“比例和布局”设置为100%。 我正试图用C#截取每个显示器的屏幕截图。 但是,我的缩放监视器报告了错误的坐标: 一切都减半了。 因此,主监视器的屏幕截图仅为屏幕面积的1/4(宽度/2,高度/2) 这是我的密码: using System; using System.Collections.Generic; using Sy

我正在尝试在我的Windows10机器上拍摄每个屏幕的截图。 我的系统有两个监视器,它们具有多个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”,我会得到不同的值:

如果我不设置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>

对/下午

如评论中所述,解决方案是将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>

对/下午

如果在控制台应用程序(在.NET 4.7上)中运行您的代码,我会得到预期的结果(两个监视器的屏幕截图都是正确的)。谢谢@Evk,您的设置中哪个监视器缩放了DPI?主要的还是次要的?我认为主要的是规模化的可能很重要。如果是主应用程序被缩放,您是使用带“”的清单,还是仅使用默认值?我使用默认值(不接触清单),主应用程序被缩放到150%,次应用程序(较小)被缩放到100%(请注意,应用程序是控制台应用程序,而不是winforms,如果这很重要的话)。我真的很迷惑为什么有人会问这个问题,知道清单很重要,但认为不显示他使用的清单是个好主意。这是错误的。@Evk我的应用程序也是一个控制台应用程序。我试着将我较大的初选规模扩大到150%(而不是200%),结果也出现了同样的错误。还有一个问题:我的小学在左边(x=0),中学在右边(x=2880)。你的设置是否相同?如果在控制台应用程序(在.NET 4.7上)中运行你的代码,我会得到预期的结果(两个监视器的正确屏幕截图)。谢谢@Evk,你的设置中哪个监视器缩放了DPI?主要的还是次要的?我认为主要的是规模化的可能很重要。如果是主应用程序被缩放,您是使用带“”的清单,还是仅使用默认值?我使用默认值(不接触清单),主应用程序被缩放到150%,次应用程序(较小)被缩放到100%(请注意,应用程序是控制台应用程序,而不是winforms,如果这很重要的话)。我真的很迷惑为什么有人会问这个问题,知道清单很重要,但认为不显示他使用的清单是个好主意。这是错误的。@Evk我的应用程序也是一个控制台应用程序。我试着将我较大的初选规模扩大到150%(而不是200%),结果也出现了同样的错误。还有一个问题:我的小学在左边(x=0),中学在右边(x=2880)。您的设置是否相同?这是唯一适用于我的CopyFromScreen解决方案,IMHO应标记为正确答案。这是唯一适用于我的CopyFromScreen解决方案,IMHO应标记为正确答案。