Fastest way to take a screenshot with python on windows

后端 未结 2 741
粉色の甜心
粉色の甜心 2020-11-27 03:17

What\'s the fastest way to take a screenshot on windows? PIL.ImageGrab is rather slow.. it takes between 4-5 seconds to take 30 screenshots of the same small wi

2条回答
  •  独厮守ぢ
    2020-11-27 03:27

    You could use win32 APIs directly .

    1) First give the focus to the App that you want to take screenshot of. link text

    2) Win32 API can help with the screenshot:

    import win32gui
    import win32ui 
    hwnd = win32gui.FindWindow(None, windowname)
    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)
    dataBitMap.SaveBitmapFile(cDC, bmpfilenamename)
    # Free Resources
    dcObj.DeleteDC()
    cDC.DeleteDC()
    win32gui.ReleaseDC(hwnd, wDC)
    win32gui.DeleteObject(dataBitMap.GetHandle())
    

提交回复
热议问题