-
Notifications
You must be signed in to change notification settings - Fork 22
/
getscreen.py
99 lines (82 loc) · 2.65 KB
/
getscreen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import time
import win32gui, win32ui, win32con, win32api
import ctypes.wintypes
def get_current_size(hwnd):
try:
f = ctypes.windll.dwmapi.DwmGetWindowAttribute
except WindowsError:
f = None
if f:
rect = ctypes.wintypes.RECT()
DWMWA_EXTENDED_FRAME_BOUNDS = 9
f(ctypes.wintypes.HWND(hwnd),
ctypes.wintypes.DWORD(DWMWA_EXTENDED_FRAME_BOUNDS),
ctypes.byref(rect),
ctypes.sizeof(rect)
)
size = (rect.right - rect.left, rect.bottom - rect.top)
return size
def gethwnd(hwname):
hwnd_title = dict()
def get_all_hwnd(hwnd, mouse):
if win32gui.IsWindow(hwnd) and win32gui.IsWindowEnabled(hwnd) and win32gui.IsWindowVisible(hwnd):
hwnd_title.update({hwnd: win32gui.GetWindowText(hwnd)})
win32gui.EnumWindows(get_all_hwnd, 0)
for h, t in hwnd_title.items():
# if t is not "":
# print(h, t)
if t == hwname:
return h
print(f"{hwname} 未找到")
return 0
def window_capture(hw, filename, mode,FULLRES):
hwnd = hw # 窗口的编号,0号表示当前活跃窗口
# 根据窗口句柄获取窗口的设备上下文DC(Divice Context)
if mode == "window":
sizeobj = get_current_size(hwnd)
else:
hwnd=0
hw=0
sizeobj = FULLRES
hwndDC = win32gui.GetWindowDC(hwnd)
# 根据窗口的DC获取mfcDC
mfcDC = win32ui.CreateDCFromHandle(hwndDC)
# mfcDC创建可兼容的DC
saveDC = mfcDC.CreateCompatibleDC()
# 创建bigmap准备保存图片
saveBitMap = win32ui.CreateBitmap()
w = sizeobj[0]
h = sizeobj[1]
# print(w,h)
catchw = w
catchh = h
startw=0
starth=0
if w==1924 and h==1130:
startw=2
starth=1130-1080
catchw=1922
catchh=1130
w=1920
h=1080
# print w,h #图片大小
# 为bitmap开辟空间
saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)
# 高度saveDC,将截图保存到saveBitmap中
saveDC.SelectObject(saveBitMap)
# 截取从左上角(0,0)长宽为(w,h)的图片
saveDC.BitBlt((0, 0), (w, h), mfcDC, (startw, starth), win32con.SRCCOPY)
# bmbit=saveBitMap.GetBitmapBits(False)
saveBitMap.SaveBitmapFile(saveDC, filename)
win32gui.DeleteObject(saveBitMap.GetHandle())
saveDC.DeleteDC()
return w, h
def getpicture(imgname, mode,FULLRES):
hw = gethwnd("Grand Theft Auto V")
return window_capture(hw, imgname, mode,FULLRES)
if __name__ == "__main__":
beg = time.time()
hw = gethwnd("Grand Theft Auto V")
window_capture(hw, "screen.bmp")
end = time.time()
print(end - beg)