0

这是一个使用 BitBlt 的相当标准的屏幕截图功能,可以在网上找到:

主功能:

while(true)
{
    printscreen = GetDesktopImage(X, Y, secMonitorSize.Width, secMonitorSize.Height);
    Thread.Sleep(1000);
}

捕获桌面功能:

public Bitmap GetDesktopImage(int X, int Y, int width, int height)
{
    IntPtr hDC = WIN32_API.GetDC(WIN32_API.GetDesktopWindow());
    IntPtr hMemDC = WIN32_API.CreateCompatibleDC(hDC);
    IntPtr m_HBitmap = WIN32_API.CreateCompatibleBitmap(hDC, width, height);

    if (m_HBitmap != IntPtr.Zero)
    {
        IntPtr hOld = (IntPtr)WIN32_API.SelectObject(hMemDC, m_HBitmap);
        WIN32_API.BitBlt(hMemDC, 0, 0, width, height, hDC, X, Y, WIN32_API.SRCCOPY | WIN32_API.CAPTURE_BLT);

        WIN32_API.SelectObject(hMemDC, hOld);
        WIN32_API.DeleteDC(hMemDC);
        WIN32_API.ReleaseDC(WIN32_API.GetDesktopWindow(), hDC);

        Bitmap printscreen = System.Drawing.Image.FromHbitmap(m_HBitmap);
        WIN32_API.DeleteObject(m_HBitmap);

        return printscreen;
    }

    return null;
}

问题是代码可以正常运行大约 20 分钟,然后 CreateCompatibleBitmap 将继续返回 0。在 CreateCompatibleBitmap 上使用 setlasterror=true,它会显示错误代码 997(Overlapped I/O Operation Is In Progress)。

只有赛门铁克在后台运行。任何人都知道如何开始故障排除?

4

1 回答 1

3

GDI 函数不使用GetLastError(),因此 usingsetlasterror=true将报告早期 API 调用的错误。

尝试这个:

public Bitmap GetDesktopImage(int X, int Y, int width, int height)
{
    Bitmap printscreen = null;

    IntPtr hWnd = WIN32_API.GetDesktopWindow();
    IntPtr hDC = WIN32_API.GetDC(hWnd);
    if (hDC != IntPtr.Zero)
    { 
        IntPtr hMemDC = WIN32_API.CreateCompatibleDC(hDC);
        if (hMemDC != IntPtr.Zero)
        {
            IntPtr m_HBitmap = WIN32_API.CreateCompatibleBitmap(hDC, width, height);
            if (m_HBitmap != IntPtr.Zero)
            {
                IntPtr hOld = (IntPtr)WIN32_API.SelectObject(hMemDC, m_HBitmap);
                WIN32_API.BitBlt(hMemDC, 0, 0, width, height, hDC, X, Y, WIN32_API.SRCCOPY | WIN32_API.CAPTURE_BLT);
                WIN32_API.SelectObject(hMemDC, hOld);

                printscreen = System.Drawing.Image.FromHbitmap(m_HBitmap);
                WIN32_API.DeleteObject(m_HBitmap);
            }

            WIN32_API.DeleteDC(hMemDC);
        }

        WIN32_API.ReleaseDC(hWnd, hDC);
    }

    return printscreen;
}
于 2013-01-17T08:20:29.680 回答