简体   繁体   English

仅截图窗口的特定区域

[英]Only screenshot a specific area of a window

I'm currently screenshotting windows using the following lines of code: 我目前正在使用以下代码行截图Windows:

UpdateWindow(hwnd);
HDC window_dc = GetDC(hwnd);
HDC res = CreateCompatibleDC(window_dc);
RECT r;
GetClientRect(hwnd, &r);
HBITMAP bmp = CreateCompatibleBitmap(window_dc, r.right - r.left, r.bottom - r.top);
SelectObject(res, bmp);
PrintWindow(hwnd, res, PW_CLIENTONLY);
DeleteObject(bmp);
ReleaseDC(hwnd, window_dc);

Now is there a way, to only screenshot a specific area of my HWND? 现在有办法只截取HWND的特定区域吗? I realized, that on some applications, the screenshotting takes siginificantly longer when the window is only a little bit larger. 我意识到,在某些应用程序中,当窗口仅大一点时,屏幕截图的时间就显着延长了。 So I figured if I was able to only screenshot the important frame, I could really improve my speed. 因此,我想出了如果只能对重要的框架进行屏幕截图,则可以真正提高速度。

EDIT: It needs to work for windows which are in the background, eg. 编辑:它需要工作在后台的窗口,例如。 overlapped by other windows. 被其他窗口重叠。

You could try it like this: 您可以这样尝试:

bool ScreenShot(HWND hwnd, int x, int y, int w, int h, LPCSTR file){
    HDC source = GetDC(hwnd);
    HDC memory = CreateCompatibleDC(source);

    HBITMAP bitmap = CreateCompatibleBitmap(source, w, h);
    HBITMAP bitmapOld = (HBITMAP)SelectObject(memory, hBitmap);

    BitBlt(memory, 0, 0, w, h, source, x, y, SRCCOPY);
    hBitmap = (HBITMAP)SelectObject(memory, bitmapOld);

    DeleteDC(source);
    DeleteDC(memory);

    HPALETTE pal = NULL;
    if(saveBitmap(file, bitmap, pal)) return true;
    return false;
}

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

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