我正在尝试使用 pywin32 模块来获取窗口客户区的位图。使用这个问题的确切代码,我可以获得整个桌面的位图。具体来说,当我查看返回的位时,有 4*(desktop-pixels) 条目数。但是,当我尝试为窗口或客户区做类似的事情时,我得到的返回条目比像素少。我获取窗口位图的代码如下。但是,我已经尝试了几种变体。例如,用客户区替换窗口部件,将 (nXSrc, nYSrc) 设置为零等。关于我可能做错的任何建议?非常感谢!
代码:
windowName = "MyProcessWindow"
windowHandle = win32ui.FindWindow(None, windowName).GetSafeHwnd()
windowRectangle = win32gui.GetWindowRect(windowHandle)
width = windowRectangle[2]-windowRectangle[0]
height = windowRectangle[3]-windowRectangle[1]
win32gui.SetForegroundWindow(windowHandle)
windowDeviceContext = win32gui.GetWindowDC(windowHandle)
deviceContextObject = win32ui.CreateDCFromHandle(windowDeviceContext)
compatibleDeviceContext = deviceContextObject.CreateCompatibleDC()
dataBitMap = win32ui.CreateBitmap()
dataBitMap.CreateCompatibleBitmap(compatibleDeviceContext, width, height)
compatibleDeviceContext.SelectObject(dataBitMap)
compatibleDeviceContext.BitBlt((0,0), (width, height), deviceContextObject, (windowRectangle[0],windowRectangle[1]), win32con.SRCCOPY)
info = dataBitMap.GetInfo()
bits = dataBitMap.GetBitmapBits(False)
print info
print len(bits)
print (width*height)
打印结果:
{'bmType': 0, 'bmWidthBytes': 92, 'bmHeight': 526, 'bmBitsPixel': 1, 'bmPlanes': 1, 'bmWidth': 728}
48392
382928