简体   繁体   English

屏幕关闭时捕获窗口

[英]Capture window when screen is off

I'm using Native GDI+ for capture window "over screen". 我正在使用Native GDI +捕获窗口“屏幕上”。 It's not working when screen is off (generates black image). 屏幕关闭时它不起作用(生成黑色图像)。 How can I fix it? 我该如何解决? (I'm using .Net 4.5) (我正在使用.Net 4.5)

public static Image CaptureWindow(IntPtr handle)
{
        IntPtr hdcSrc = User32.GetWindowDC(handle);

        RECT windowRect = new RECT();
        User32.GetWindowRect(handle, ref windowRect);

        int width = windowRect.right - windowRect.left;
        int height = windowRect.bottom - windowRect.top;

        IntPtr hdcDest = Gdi32.CreateCompatibleDC(hdcSrc);
        IntPtr hBitmap = Gdi32.CreateCompatibleBitmap(hdcSrc, width, height);

        IntPtr hOld = Gdi32.SelectObject(hdcDest, hBitmap);
        Gdi32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, SRCCOPY);
        Gdi32.SelectObject(hdcDest, hOld);
        Gdi32.DeleteDC(hdcDest);
        User32.ReleaseDC(handle, hdcSrc);

        Image image = Image.FromHbitmap(hBitmap);
        Gdi32.DeleteObject(hBitmap);

        return image;
 }

As far as i know the graphics driver stops producing an image when there is no one to receive it, to save power. 据我所知,图形驱动程序在没有人接收时停止生成图像,以节省电量。 This might be why you can't capture the image when the screen is "off". 这可能是您在屏幕“关闭”时无法捕获图像的原因。 There is no point in using CPU and GPU and BUS power on producing an image, when there is no one to receive it. 当没有人接收图像时,在生成图像时使用CPU和GPU以及总线电源是没有意义的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM