我已經將一個簡單的python腳本放在一起,該腳本應該截取其名稱包含特定字符串的窗口。我使用的代碼如下,winAPI BitBlt故障捕獲窗口僅適用於某些應用程序
import win32gui, win32ui, win32con
import PIL.Image
def getWindowHandle(name):
windowList = []
win32gui.EnumWindows(lambda hwnd, wndList:
wndList.append((win32gui.GetWindowText(hwnd), hwnd)),
windowList)
for pair in windowList:
if name in pair[0]:
return pair[1]
return None
class Window():
def __init__(self, hwnd = None):
if not hwnd: return
l, t, r, b = win32gui.GetClientRect(hwnd)
sl, st, _, _ = win32gui.GetWindowRect(hwnd)
cl, ct = win32gui.ClientToScreen(hwnd, (l, t))
self.size = (r - l, b - t)
self.position = (cl - sl, ct - st)
hDC = win32gui.GetWindowDC(hwnd)
self.windowDC = win32ui.CreateDCFromHandle(hDC)
self.newDC = self.windowDC.CreateCompatibleDC()
#win32gui.ReleaseDC(hwnd, hDC)
self.bitmap = win32ui.CreateBitmap()
self.bitmap.CreateCompatibleBitmap(self.windowDC, self.size[0], self.size[1])
self.newDC.SelectObject(self.bitmap)
def __del__(self):
self.newDC.DeleteDC()
self.windowDC.DeleteDC()
del self.bitmap
def screenshot(self, location = 'C:\\Users\\Grieverheart\\Desktop\\'):
self.newDC.BitBlt((0, 0), self.size, self.windowDC, self.position, win32con.SRCCOPY)
self.bitmap.Paint(self.newDC)
bmpinfo = self.bitmap.GetInfo()
bmpstr = self.bitmap.GetBitmapBits(True)
im = PIL.Image.frombuffer('RGB', self.size, bmpstr, 'raw', 'BGRX', 0, 1)
try:
im.save(location + 'test.png', 'PNG')
except IOError:
return
def main():
handle = getWindowHandle("Blender")
if not handle: return
window = Window(handle)
window.screenshot()
if __name__ == "__main__":
main()
該腳本爲我提供了一些應用程序的黑色截圖,如Blender或DOSBox。
有誰知道是什麼原因導致這個問題只有一些應用程序,我怎麼能解決它?
編輯:這似乎我的問題可能與this後,雖然我不知道我必須做什麼來解決我的問題。我還想補充一點,我也嘗試添加CAPTUREBLT標誌,沒有任何區別。
嘗試添加CAPTUREBLT標誌。 – Xearinox
您是否在啓用Aero的情況下使用Windows Vista/7? –