蟒蛇 - pywin32得到客户区的位图? [英] Python - pywin32 getting bitmap of client area?

查看:217
本文介绍了蟒蛇 - pywin32得到客户区的位图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用pywin32模块得到一个窗口的客户区位图。使用完全相同code从这个问题我能够获得整个的位图桌面。具体而言,当我看回位,有4 *(桌面像素)参赛人数。然而,当我尝试做一个窗口或客户区类似的东西,我得到更少的条目返回再就是像素。我的code获取窗口位图如下。不过,我已经试过了好变化。例如,客户区更换窗口部位,设置(nXSrc,nYSrc)零等对我可能是做错了什么建议?谢谢了!

code:

  windowName =MyProcessWindow
windowHandle = win32ui.FindWindow(无,windowName).GetSafeHwnd()
windowRectangle = win32gui.GetWindowRect(windowHandle)
宽度= windowRectangle [2] -windowRectangle [0]
高度= windowRectangle [3] -windowRectangle [1]
win32gui.SetForegroundWindow(windowHandle)
windowDeviceContext = win32gui.GetWindowDC(windowHandle)
deviceContextObject = win32ui.CreateDCFromHandle(windowDeviceContext)
compatibleDeviceContext = deviceContextObject.CreateCompatibleDC()
dataBitMap = win32ui.CreateBitmap()
dataBitMap.CreateCompatibleBitmap(compatibleDeviceContext,宽度,高度)
compatibleDeviceContext.SelectObject(dataBitMap)
compatibleDeviceContext.BitBlt((0,0),(宽度,高度),deviceContextObject,(windowRectangle [0],windowRectangle [1]),win32con.SRCCOPY)
信息= dataBitMap.GetInfo()
位= dataBitMap.GetBitmapBits(假)
打印信息
打印LEN​​(位)
打印(宽*高)

打印结果:

  {'bmType':0,'bmWidthBytes:92,bmHeight':526,'bmBitsPixel':1,'bmPlanes':1,'bmWidth:728}
48392
382928


解决方案

我认为这个问题是这样的:

  dataBitMap.CreateCompatibleBitmap(compatibleDeviceContext,宽,高)

您想创建一个位图与兼容的的DC,其具有相应的色深和其他功能。然后你可以选择到目标DC,它会给的的相应颜色的深浅,大小和其他特性。

相反,你创建一个是用的目标的DC,而在这一点上是一个1x1的黑色&安培兼容;白色的DC,作为的 CreateCompatibleDC 文档。然后你选择到目标DC,离开它,正是因为它开始了。

这就是为什么你最终以1平面1bpp的位图。

查看捕捉图片样本(在C,在那个可怕的C,但希望你能理解)对于如何做到这一点的例子。但我相信这是你做错了唯一的一步。

I'm trying to use the pywin32 module to get the bitmap of a window's client area. Using the exact code from this question I am able to get the bitmap of the entire desktop. Specifically, when I look at the bits returned, there are 4*(desktop-pixels) number of entries. However, when I try to do something similar for a window or client area, I get less entries returned then there are pixels. My code for getting the window bitmap is below. However, I have tried several variations on it. For example, replacing the window parts with the client area, setting the (nXSrc, nYSrc) to zero, etc. Any suggestions on what I might be doing wrong? Thank you much!

Code:

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)

Print results:

{'bmType': 0, 'bmWidthBytes': 92, 'bmHeight': 526, 'bmBitsPixel': 1, 'bmPlanes': 1, 'bmWidth': 728}
48392
382928

解决方案

I think the problem is this:

dataBitMap.CreateCompatibleBitmap(compatibleDeviceContext, width, height)

You want to create a bitmap that's compatible with the source DC, which has the appropriate color depth and other features. Then you can select that into the target DC, which will give it the appropriate color depth, size, and other features.

Instead, you're creating one that's compatible with the target DC, which at this point is a 1x1 black&white DC, as explained in the CreateCompatibleDC docs. Then you select that into the target DC, leaving it exactly as it started.

That's why you end up with a bitmap with 1 plane of 1bpp.

See the Capturing a Image sample (in C, and horrible C at that, but hopefully you can follow it) for an example of how to do it. But I believe this is the only step you're doing wrong.

这篇关于蟒蛇 - pywin32得到客户区的位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆