我想捕获特定区域的python

时间:2020-11-04 02:39:44

标签: rgb win32gui

def capture(hwnd):
    x1, y1, x2, y2 = win32gui.GetClientRect(hwnd)
    w = x2 - x1
    h = y2 - y1
    hwndDC = win32gui.GetWindowDC(hwnd)
    mfcDC = win32ui.CreateDCFromHandle(hwndDC)
    saveDC = mfcDC.CreateCompatibleDC()
    saveBitMap = win32ui.CreateBitmap()
    saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)
    saveDC.SelectObject(saveBitMap)
    result = windll.user32.PrintWindow(hwnd, saveDC.GetSafeHdc(), 0)
    bmpinfo = saveBitMap.GetInfo()
    bmpstr = saveBitMap.GetBitmapBits(True)
    im = Image.frombuffer(
    'RGB',
    (bmpinfo['bmWidth'], bmpinfo['bmHeight']),
    bmpstr, 'raw', 'RGBA', 0, 1
    )
    win32gui.DeleteObject(saveBitMap.GetHandle())
    saveDC.DeleteDC()
    mfcDC.DeleteDC()
    win32gui.ReleaseDC(hwnd, hwndDC)
    return im

我只想捕获手柄窗口内的所需区域 我只想捕获手柄窗口内的所需区域 我只想捕获手柄窗口内的所需区域 我只想捕获手柄窗口内的所需区域

0 个答案:

没有答案