VB 屏幕截图的比例表格

VB Scale form for screen capture

我正在尝试使用以下代码进行屏幕截图

    Dim area As Rectangle = FormPrintArea.Bounds
    Dim areaWidth As Integer = CInt(area.Width * ScaleFactor)
    Dim areaHeight As Integer = CInt(area.Height * ScaleFactor)
    Dim capture As Bitmap = New Bitmap(areaWidth, areaHeight, PixelFormat.Format32bppArgb)
    Dim graph As Graphics = Graphics.FromImage(capture)
    Dim ScaledSize As New Size With {
        .Height = areaHeight,
        .Width = areaWidth
    }
    graph.CopyFromScreen(area.X, area.Y, 0, 0, ScaledSize, CopyPixelOperation.SourceCopy)
    PictureBox1.Image = capture

`` 问题是我找不到 ScaleFactor,只要 "Change the size of text, apps and the other items: 100%" in Windows 10 Display Settings 一切正常,但如果设置不同,如 125%(推荐),我会损失大约 20%图片。 LanguageFont class 中似乎有一个 ScaleFactor,但我似乎无法从 VB(或 C#)访问它。

该应用程序有一个 VB 表单 (FormPrintArea),用户可以使用它来定义打印区域。如果我在设置为 125%(推荐)的系统上将 ScaleFactor 设置为 1.25,那么一切正常。有什么方法可以通过 API 从 Windows 中获取值吗?

正如@Hans Passant 所说,我需要声明我的应用程序 DPIaware,最简单的方法是将以下行添加到部分 Class 启动表单 (Form1)。

<DllImport("User32.dll")> Private Shared Sub SetProcessDPIAware() End Sub

然后把启动窗体最后的New Sub改成调用SetProcessDPIAware

Public Sub New() SetProcessDPIAware() InitializeComponent() End Sub

一旦你这样做了,你就可以在 Form1.Load 中获得 ScaleFactor,如下所示 DPIScalingX = Me.CreateGraphics().DpiX / 96 DPIScalingY = Me.CreateGraphics().DpiY / 96