简体   繁体   中英

Pywin32, win32gui window capture black screen

I need to make a screen capture of a specific application. If I grab the Windows desktop ( hwnd = None ), everything works, but I get a black screen when I try to grab the screen of a specific application, eg hwnd = win32gui.FindWindow(None, 'Albion Online Client') .

import cv2 as cv
import numpy as np
import os
from time import time
import win32gui
import win32ui
import win32con

os.chdir(os.path.dirname(os.path.abspath(__file__)))

def window_capture():
    w = 1920  # set this
    h = 1080  # set this

    #hwnd = None ### Everything works
    hwnd = win32gui.FindWindow(None, 'Albion Online Client') ### Black Screen
    wDC = win32gui.GetWindowDC(hwnd)
    dcObj = win32ui.CreateDCFromHandle(wDC)
    cDC = dcObj.CreateCompatibleDC()
    dataBitMap = win32ui.CreateBitmap()
    dataBitMap.CreateCompatibleBitmap(dcObj, w, h)
    cDC.SelectObject(dataBitMap)
    cDC.BitBlt((0, 0), (w, h), dcObj, (0, 0), win32con.SRCCOPY)

    #Save screenshoot
    #dataBitMap.SaveBitmapFile(cDC, 'debug.bmp' )
    signedIntsArray = dataBitMap.GetBitmapBits(True)
    img = np.fromstring(signedIntsArray, dtype='uint8')
    img.shape = (h, w, 4)

    # Free Resources
    dcObj.DeleteDC()
    cDC.DeleteDC()
    win32gui.ReleaseDC(hwnd, wDC)
    win32gui.DeleteObject(dataBitMap.GetHandle())

    img = img[..., :3]

    img = np.ascontiguousarray(img)
    return img
# initialize the WindowCapture clas

loop_time = time()
while(True):
  
    screenshot = window_capture()
    
    cv.imshow('Computer Vision', screenshot)

    # debug the loop rate
    print('FPS {}'.format(1 / (time() - loop_time)))
    loop_time = time()

    # press 'q' with the output window focused to exit.
    # waits 1 ms every loop to process key presses
    if cv.waitKey(1) == ord('q'):
        cv.destroyAllWindows()
        break

print('Done.')

That's an OpenGL or Direct3D application. It doesn't draw to its own window. It creates textures and surfaces, and has the graphics card render its display list directly into the frame buffer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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