高DPI机器上的屏幕截图

时间:2019-03-29 12:57:17

标签: c# screenshot highdpi

我正在使用以下代码来捕获桌面屏幕快照。它在正常的DPI计算机上工作正常。但是,如果DPI设置高于正常水平,则捕获图像中不会显示部分屏幕。

经过一些研究,我知道我需要在应用清单文件中添加以下设置。

遵循此链接中指定的步骤 configure-an-app-to-run-correctly-on-a-machine-with-a-high-dpi-setting

但是问题仍然存在。捕获屏幕在高DPI机器上不正确。

请指导我。要克服这个问题还需要做些什么。

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



static Bitmap CaptureDesktop()
    {
        SIZE size;
        Bitmap printscreen = null;

        size.cx = Win32Stuff.GetSystemMetrics
                         (Win32Stuff.SM_CXSCREEN);

        size.cy = Win32Stuff.GetSystemMetrics
                  (Win32Stuff.SM_CYSCREEN);

        int width = size.cx; int height = size.cy;

        IntPtr hWnd = Win32Stuff.GetDesktopWindow();
        IntPtr hDC = Win32Stuff.GetDC(hWnd);
        if (hDC != IntPtr.Zero)
        {
            IntPtr hMemDC = GDIStuff.CreateCompatibleDC(hDC);
            if (hMemDC != IntPtr.Zero)
            {
                IntPtr m_HBitmap = GDIStuff.CreateCompatibleBitmap(hDC, width, height);
                if (m_HBitmap != IntPtr.Zero)
                {
                    IntPtr hOld = (IntPtr)GDIStuff.SelectObject(hMemDC, m_HBitmap);
                    GDIStuff.BitBlt(hMemDC, 0, 0, width, height, hDC, 0, 0, GDIStuff.SRCCOPY);
                    GDIStuff.SelectObject(hMemDC, hOld);
                    GDIStuff.DeleteDC(hMemDC);
                    printscreen = System.Drawing.Image.FromHbitmap(m_HBitmap);
                    GDIStuff.DeleteObject(m_HBitmap);
                }
            }
        }
        Win32Stuff.ReleaseDC(hWnd, hDC);

        return printscreen;
    }

0 个答案:

没有答案