I'm trying to get a full screenshots from an application window even when minimized, maximized, or any window shape. I've looked at other questions like this but havn't found the answer that I'm looking for.
I've tried the code below and it works, but has limited capability with what I want it to do.
def screenshot(hwnd = None):
left, top, right, bot = win32gui.GetWindowRect(hwnd)
w = right - left
h = bot - top
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', 'BGRX', 0, 1)
win32gui.DeleteObject(saveBitMap.GetHandle())
saveDC.DeleteDC()
mfcDC.DeleteDC()
win32gui.ReleaseDC(hwnd, hwndDC)
if result == 1:
#PrintWindow Succeeded
im.save(r"c:\python27\programs\check.bmp")
Using this code with a window that is maximized yields a great result!
But when the window is shrinked.......not so much
I tried editing this line but end up with an akward result :/ . saveBitMap.CreateCompatibleBitmap(mfcDC, w+100, h+100)
Does anyone know how to take a screenshot of a fully windowed application without maximizing then windowing again? Maybe something along the lines of using win32con.SW_MAXIMIZE
.
Why not use pywinauto + Pillow / PIL?