python使用win32api进行后台窗口的部分截图函数

def window_capture_beat(hwnd,stayx:int,endx:int,stay:int,endy:int):

    hwndDC = win32gui.GetWindowDC(hwnd)
    mfcDC = win32ui.CreateDCFromHandle(hwndDC)
    saveDC = mfcDC.CreateCompatibleDC()
    saveBitMap = win32ui.CreateBitmap()
    width = endx - stayx
    height = endy - stay
    saveBitMap.CreateCompatibleBitmap(mfcDC, width, height)
    saveDC.SelectObject(saveBitMap)
    saveDC.BitBlt((0, 0), (width, height), mfcDC, (stayx, stay), win32con.SRCCOPY)
    signedIntsArray = saveBitMap.GetBitmapBits(True)
    win32gui.DeleteObject(saveBitMap.GetHandle())
    mfcDC.DeleteDC()
    saveDC.DeleteDC()
    win32gui.ReleaseDC(hwnd, hwndDC)
    img = np.frombuffer(signedIntsArray, dtype="uint8")
    img.shape = (height, width, 4)
    return cv2.cvtColor(img, cv2.COLOR_RGBA2RGB)

  

posted @ 2022-09-29 17:22  小二君i  阅读(1056)  评论(0)    收藏  举报