gpt4 book ai didi

任意大小的 Python 屏幕截图应用程序窗口

转载 作者:太空宇宙 更新时间:2023-11-03 14:18:41 25 4
gpt4 key购买 nike

我正在尝试从应用程序窗口获取完整的屏幕截图,即使是最小化、最大化或任何窗口形状。我看过其他问题,例如 this但还没有找到我正在寻找的答案。

我已经尝试了下面的代码并且它可以工作,但是我想要它做的事情的能力有限。

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")

将此代码与最大化的窗口一起使用会产生很好的结果! enter image description here

但是当窗口缩小时.......没有那么多

enter image description here

我尝试编辑这一行,但结果很糟糕:/。 saveBitMap.CreateCompatibleBitmap(mfcDC, w+100, h+100) enter image description here

有谁知道如何在不最大化然后再次打开窗口的情况下截取全窗口应用程序的屏幕截图?可能类似于使用 win32con.SW_MAXIMIZE

最佳答案

为什么不使用 pywinauto + Pillow / PIL

from pywinauto import application
app = application.Application().start("notepad.exe")
app.Untitled_Notepad.capture_as_image().save('window.png')

关于任意大小的 Python 屏幕截图应用程序窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30846493/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com