具有不同显示比例问题的C#WPF显示屏幕截图

具有不同显示比例问题的C#WPF显示屏幕截图,c#,wpf,bitmap,screenshot,C#,Wpf,Bitmap,Screenshot,我有一个应用程序,它可以截取每个可用显示的屏幕,并根据图像将其放置在边框(image-textblock)中。当“我的所有屏幕”在Windows显示设置中将显示比例设置为100%时,一切都正常工作,但是如果将一个屏幕设置为不同的比例设置,则看起来很奇怪 以下所有缩放比例均为100%-完美- 主屏幕上的缩放比例为150%,其他两个屏幕的缩放比例为100% 以下是所有屏幕的150% 所以很明显,当一个屏幕作为不同的缩放设置时,问题就出现了。。 我该怎么纠正呢 这是我的密码: public Sc

我有一个应用程序,它可以截取每个可用显示的屏幕,并根据图像将其放置在边框(image-textblock)中。当“我的所有屏幕”在Windows显示设置中将显示比例设置为100%时,一切都正常工作,但是如果将一个屏幕设置为不同的比例设置,则看起来很奇怪

以下所有缩放比例均为100%-完美-

主屏幕上的缩放比例为150%,其他两个屏幕的缩放比例为100%

以下是所有屏幕的150%

所以很明显,当一个屏幕作为不同的缩放设置时,问题就出现了。。 我该怎么纠正呢

这是我的密码:

public ScreensListDesignModel()
{
    Items = new List<ScreensItemViewModel>();
    try
    {
        int i = 1;
        foreach (Screen screen in Screen.AllScreens)
        {
            #region Grab the screenshot from each display
            // Define bitmap
            Bitmap screenshot = new Bitmap(screen.Bounds.Width, 
                screen.Bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            // Define graphics object
            Graphics memoryGraphics = Graphics.FromImage(screenshot);
            memoryGraphics.CopyFromScreen(screen.Bounds.X, 
                screen.Bounds.Y, 0, 0, screen.Bounds.Size, CopyPixelOperation.SourceCopy);

            #endregion

            #region Get screens info and display the screenshots in each imagebox

            System.Windows.Application.Current.Dispatcher.Invoke(() =>
            {
                string SN = "Screen " + i;
                if (screen.Primary)
                    SN = "Main Screen";

                Items.Add(new ScreensItemViewModel
                {
                    Screenshot = CreateBitmapSourceFromGdiBitmap(screenshot),
                    ScreenName = SN,
                });
            });

            i++;
            screenshot.Dispose();

            #endregion
        }
    }
    catch (System.ComponentModel.Win32Exception) { } // action?
    catch (System.NullReferenceException) { } // not sure
    catch (System.Threading.Tasks.TaskCanceledException) { throw; };
}

实际上,修复非常简单,只需添加一个app.manifest文件,其中包含以下内容:

  <application xmlns="urn:schemas-microsoft-com:asm.v3">
    <windowsSettings>
      <dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2</dpiAwareness>
    </windowsSettings>
  </application>

PerMonitorV2
  <application xmlns="urn:schemas-microsoft-com:asm.v3">
    <windowsSettings>
      <dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2</dpiAwareness>
    </windowsSettings>
  </application>