-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetScreen.py
More file actions
126 lines (87 loc) · 3.23 KB
/
Copy pathgetScreen.py
File metadata and controls
126 lines (87 loc) · 3.23 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import threading
import cv2
import win32gui
import win32ui
import win32con
import win32api
import numpy as np
from ctypes import windll
from PIL import Image
# name = "Google Chrome"
# l = []
# def winEnumHandler(hdw, ctx):
# if win32gui.IsWindowVisible(hdw):
# print(hex(hdw), win32gui.GetWindowText(hdw))
# if name in str(win32gui.GetWindowText(hdw)):
# l.append(hdw)
# win32gui.EnumWindows(winEnumHandler, None)
# window_handler = l[0]
# win32gui.SetForegroundWindow(window_handler)
# win32gui.BringWindowToTop(window_handler)
# GetWindowDC() for entir window
if __name__ == "__main__":
print("This is a module import it to use it")
exit()
class Capture:
lock = threading.Lock()
def getCaptureImage(window_handler: int):
Capture.lock.acquire()
try:
# hwin = win32gui.GetDesktopWindow()
HDC = win32gui.GetWindowDC(window_handler)
mfcDC = win32ui.CreateDCFromHandle(HDC)
saveDC = mfcDC.CreateCompatibleDC()
left, top, right, bot = 0, 0, 0, 0
if window_handler == 0:
left, top, right, bot = win32gui.GetWindowRect(
win32gui.GetDesktopWindow())
else:
left, top, right, bot = win32gui.GetWindowRect(window_handler)
w = right - left
h = bot - top
saveBitMap = win32ui.CreateBitmap()
saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)
saveDC.SelectObject(saveBitMap)
result = saveDC.BitBlt((0, 0), (w, h), mfcDC,
(0, 0), win32con.SRCCOPY)
# result = windll.user32.PrintWindow(
# window_handler, saveDC.GetSafeHdc(), 1)
if result == 0:
return 0
# bmpinfo = saveBitMap.GetInfo()
# bmpstr = saveBitMap.GetBitmapBits(True)
# img = Image.frombuffer(
# 'RGB',
# (bmpinfo['bmWidth'], bmpinfo['bmHeight']),
# bmpstr, 'raw', 'BGRX', 0, 1)
signedIntsArray = saveBitMap.GetBitmapBits(True)
img = np.frombuffer(signedIntsArray, dtype='uint8')
img.shape = (h, w, 4)
# win32gui.DeleteObject(saveBitMap.GetHandle())
# saveDC.DeleteDC()
# mfcDC.DeleteDC()
# win32gui.ReleaseDC(window_handler, HDC)
def release():
if saveBitMap != None:
win32gui.DeleteObject(saveBitMap.GetHandle())
if saveDC != None:
saveDC.DeleteDC()
if mfcDC != None:
mfcDC.DeleteDC()
if window_handler != None or HDC != None:
win32gui.ReleaseDC(window_handler, HDC)
release()
Capture.lock.release()
return img
except Exception as e:
Capture.lock.release()
raise Exception(e)
def isWindowMinimized(wnhl: list) -> bool:
if wnhl == 0:
id = win32gui.GetDesktopWindow()
else:
id = wnhl
tup = win32gui.GetWindowPlacement(id)
if tup[1] == win32con.SW_SHOWMINIMIZED:
return True
return False