大家好,欢迎来到IT知识分享网。
# 需要导入模块: import win32gui [as 别名]
# 或者: from win32gui import CreateCompatibleBitmap [as 别名]
def screen(self):
“””PIL Image of current window screen. (the window must be on the top)
reference: https://msdn.microsoft.com/en-us/library/dd183402(v=vs.85).aspx”””
# opengl windows cannot get from it’s hwnd, so we use the screen
hwnd = win32gui.GetDesktopWindow()
# get window size and offset
left, top, right, bottom = self.rect
width, height = right-left, bottom-top
# the device context of the window
hdcwin = win32gui.GetWindowDC(hwnd)
# make a temporary dc
hdcmem = win32gui.CreateCompatibleDC(hdcwin)
# make a temporary bitmap in memory, this is a PyHANDLE object
hbmp = win32gui.CreateCompatibleBitmap(hdcwin, width, height)
# select bitmap for temporary dc
win32gui.SelectObject(hdcmem, hbmp)
# copy bits to temporary dc
win32gui.BitBlt(hdcmem, 0, 0, width, height,
hdcwin, left, top, win32con.SRCCOPY)
# check the bitmap object infomation
bmp = win32gui.GetObject(hbmp)
bi = BITMAPINFOHEADER()
bi.biSize = ctypes.sizeof(BITMAPINFOHEADER)
bi.biWidth = bmp.bmWidth
bi.biHeight = bmp.bmHeight
bi.biPlanes = bmp.bmPlanes
bi.biBitCount = bmp.bmBitsPixel
bi.biCompression = 0 # BI_RGB
bi.biSizeImage = 0
bi.biXPelsPerMeter = 0
bi.biYPelsPerMeter = 0
bi.biClrUsed = 0
bi.biClrImportant = 0
# calculate total size for bits
pixel = bmp.bmBitsPixel
size = ((bmp.bmWidth * pixel + pixel – 1)/pixel) * 4 * bmp.bmHeight
buf = (ctypes.c_char * size)()
# read bits into buffer
windll.gdi32.GetDIBits(hdcmem, hbmp.handle, 0, bmp.bmHeight, buf, ctypes.byref(bi), win32con.DIB_RGB_COLORS)
# make a PIL Image
img = Image.frombuffer(‘RGB’, (bmp.bmWidth, bmp.bmHeight), buf, ‘raw’, ‘BGRX’, 0, 1)
img = img.transpose(Image.FLIP_TOP_BOTTOM)
# cleanup
win32gui.DeleteObject(hbmp)
win32gui.DeleteObject(hdcmem)
win32gui.ReleaseDC(hwnd, hdcwin)
return img
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/13609.html