def screenshot(pid, client=True):
        # Get handle
        hwnd = win_for_pid(pid)
        # Get window dimensions
        rect = RECT()
        if client:
            GetClientRect(hwnd, ctypes.byref(rect))
        else:
            GetWindowRect(hwnd, ctypes.byref(rect))
        left, right, top, bottom = rect.left, rect.right, rect.top, rect.bottom
        w, h = right - left, bottom - top
        
        hwndDC = saveDC = bmp = None
        try:
            # Get device contexts
            hwndDC = GetWindowDC(hwnd)
            saveDC = CreateCompatibleDC(hwndDC)
            # Get bitmap
            bmp = CreateCompatibleBitmap(hwndDC, w, h)
            SelectObject(saveDC, bmp)
            if client:
                PrintWindow(hwnd, saveDC, 1)  # todo: result is never used??
            else:
                PrintWindow(hwnd, saveDC, 0)
            # Init bitmap info
            # We grab the image in RGBX mode, so that each word is 32bit and
            # we have no striding, then we transform to RGB
            buffer_len = h * w * 4
            bmi = BITMAPINFO()
            bmi.bmiHeader.biSize = ctypes.sizeof(BITMAPINFOHEADER)
            bmi.bmiHeader.biWidth = w
            bmi.bmiHeader.biHeight = -h  # Why minus? See [1]
            bmi.bmiHeader.biPlanes = 1  # Always 1
            bmi.bmiHeader.biBitCount = 32
            bmi.bmiHeader.biCompression = BI_RGB
            # Blit
            image = ctypes.create_string_buffer(buffer_len)
            bits = windll.gdi32.GetDIBits(saveDC, bmp, 0, h, image, bmi, DIB_RGB_COLORS)
            assert bits == h
            # Replace pixels values: BGRX to RGB
            image2 = ctypes.create_string_buffer(h*w*3)
            image2[0::3] = image[2::4]
            image2[1::3] = image[1::4]
            image2[2::3] = image[0::4]
            
            return bytes(image2), (w, h, 3)
        
        finally:
            # Clean up
            if hwndDC:
                DeleteObject(hwndDC)
            if saveDC:
                DeleteObject(saveDC)
            if bmp:
                DeleteObject(bmp)
Grab a screenshot of the first visible window of the process
dataset/ETHPy150Open zoofIO/flexx/flexx/util/screenshot.py/screenshot