使用非建议的缩放比例时,BitBlt在不同的应用程序中给出不同的结果

时间:2019-02-18 18:39:28

标签: c# pinvoke gdi

当缩放比例设置为100%以外的其他值(建议吗?)并使用应用程序窗口句柄时,根据单个应用程序,我会看到不同的结果。具体来说,cx,cy,x1和y1似乎被解释为不同。 Notepad ++将返回预期的结果,但是基于缩放比例,Thunderbird的显示将不正确。当窗口最大化时,Notepad ++和Thunderbird都报告相同的大小/位置,并且两者使用完全相同的数字。因此,所抓取图像的位置应该相同,但不同。

在Windows 10 1809上进行开发。还已在运行稍旧版本的Windows 10 VM和另一台计算机上进行了测试。 dpiAwareness设置为PerMonitor。 (PerMonitorV2没做任何更改)。 dpiAware设置为true。 gdiScaling设置为true。

删除dpiAwareness和dpiAware(仅保留gdiScaling)将导致翻转问题,其中notepad ++是正确的,而雷鸟则是相反的方向。

最坏的情况下,我可以在整个屏幕上使用BitBlt并找到窗口的相对位置。使用整个屏幕时,缩放比例可以正常工作,但是如果可能的话,偶尔使用我想抓住的窗口手柄也会有好处。

我正在使用的当前代码如下。

// NativeMethods is a class containing native code and related datatypes
private Bitmap GetImage(IntPtr handle, int top, int left, int width, int height)
{
    IntPtr hdcSrc = NativeMethods.GetWindowDC(handle);
    NativeMethods.Rect windowRect = new NativeMethods.Rect();
    NativeMethods.GetWindowRect(handle, ref windowRect);

    IntPtr hdcDest = NativeMethods.CreateCompatibleDC(hdcSrc);
    IntPtr hBitmap = NativeMethods.CreateCompatibleBitmap(hdcSrc, width, height);
    IntPtr hOld = NativeMethods.SelectObject(hdcDest, hBitmap);
    NativeMethods.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, left, top, NativeMethods.RasterOperationSrcCopy);
    NativeMethods.SelectObject(hdcDest, hOld);
    NativeMethods.DeleteDC(hdcDest);
    NativeMethods.ReleaseDC(handle, hdcSrc);
    Bitmap img = Image.FromHbitmap(hBitmap);
    NativeMethods.DeleteObject(hBitmap);
    return img;
}

0 个答案:

没有答案