Screen Capture 程序复制超过选定区域

Screen Capture program copies more than selected area

我跟随 Huw Collingbourne 程序使用 C# 创建了一个屏幕捕获程序。但是,我注意到一些奇怪的项目,无论我使用他创建的程序还是我修改的程序都一样。具体来说,我创建了一个打开 window 的程序,允许您捕获该区域。 我认为这与坐在我的电脑上有关,但如果其他人要使用我的屏幕捕获程序,我需要知道如何预测和解决这个问题! 如果我将 windows 10 的显示设置为 100%,我得到的会比选定的 window 多一点,如果我将显示设置为 125% 文本,那么我会得到很多选定区域。保留默认大小,我的大小应该是 555、484。但我捕获的更大。

public partial class Form1 : Form
{
    //https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-getwindowrect
    [DllImport("user32.dll")]
    private static extern IntPtr GetForegroundWindow();
    [DllImport("user32.dll")]
    private static extern IntPtr GetDesktopWindow();
    [DllImport("user32.dll")]
    private static extern bool GetWindowRect(IntPtr hWnd, ref Rectangle lpRect);

    //ICON info
    //https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-getcursorinfo
    [DllImport("user32.dll")]
    private static extern bool GetIconInfo(IntPtr hIcon, out ICONINFO piconinfo);
    [DllImport("user32.dll")]
    private static extern bool GetCursorInfo(out CURSORINFO pci);


    public struct POINT
    {
        public Int32 x;
        public Int32 y;
    }

    public struct ICONINFO
    {
        public bool fIcon;
        public Int32 xHotspot;
        public Int32 yHotspot;
        public IntPtr hbmMask;
        public IntPtr hbmColor;
    }

    public struct CURSORINFO
    {
        public Int32 cbSize;
        public Int32 flags;
        public IntPtr hCursor;
        public Point ptScreenPos;
    }

    GrabRegionForm grabForm;

    public void GrabRect(Rectangle rect)
    {
        int rectWidth = rect.Width - rect.Left;
        int rectHeight = rect.Height - rect.Top;
        Bitmap bm = new Bitmap(rectWidth, rectHeight);
        Graphics g = Graphics.FromImage(bm);
        g.CopyFromScreen(rect.Left, rect.Top, 0, 0, new Size(rectWidth, rectHeight));
        DrawMousePointer(g, Cursor.Position.X - rect.Left, Cursor.Position.Y - rect.Top);
        this.pb_screengrab.Image = bm;
        Clipboard.SetImage(bm);
    }
}

public partial class GrabRegionForm : Form
{
    public Rectangle formRect;
    private Form1 mainForm;
    [DllImport("user32.dll")]
    public static extern bool ReleaseCapture();
    [DllImport("user32.dll")]
    public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
    private void buttonOK_Click(object sender, EventArgs e)
    {
        formRect = new Rectangle(this.Left, this.Top, this.Left + this.Width, this.Top + this.Height);
        this.Hide();
        mainForm.GrabRect(formRect);
        Close();
    }
}

ScreenGrab with Display at 100%

ScreenGrab with Display at 125%

Area showing capture window

Area Actually Captured

如果使用早于 4.7 而不是 windows 10 遵循 Jimi 的示例并确保您注销并重新登录 windows。

来自吉米https://whosebug.com/users/7444103/jimi 如何在具有高 DPI 设置 How to configure an app to run correctly on a machine with a high DPI setting (e.g. 150%)?

的计算机上将应用程序正确配置为 运行

来自吉米https://whosebug.com/users/7444103/jimi 将 SetWindowPos 与多个监视器一起使用

如果我将我的应用程序定位为 windows 10 Only 现在非常简单。 如果使用 Windows 10.

,Microsoft 通过使用 4.7 可以更轻松地更改 DPI 设置

https://docs.microsoft.com/en-us/dotnet/framework/winforms/high-dpi-support-in-windows-forms

声明与 Windows 10 的兼容性。 然后将以下内容添加到 XML 中的 app.manifest 文件中,以便 Windows 10 compatibility.

supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"

在 app.config 文件中启用每个显示器的 DPI 感知。 Windows Forms 引入了一个新元素来支持从 .NET Framework 4.7 开始添加的新功能和自定义项。要利用支持高 DPI 的新功能,请将以下内容添加到您的应用程序配置文件中。

转到 XML 行 System.Windows.Forms.ApplicationConfigurationSection

add key="DpiAwareness" value="PerMonitorV2"