import io
import time
import struct
import ctypes
s = time.time()
gdi32 = ctypes.windll.gdi32
user32 = ctypes.windll.user32
# 定义常量
SM_CXSCREEN = 0
SM_CYSCREEN = 1
# 缩放比例
zoom = 1
screenWidth = int(user32.GetSystemMetrics(SM_CXSCREEN) * zoom)
screenHeight = int(user32.GetSystemMetrics(SM_CYSCREEN) * zoom)
# 屏幕截取
def capture_screen(x, y, width, height):
for _ in range(100):
# time.sleep(0.01)
# 获取桌面窗口句柄
hwnd = user32.GetDesktopWindow()
# 获取桌面窗口的设备上下文
hdc_src = user32.GetDC(hwnd)
if len(str(hdc_src)) > 16:
# time.sleep(0.01)
# 释放桌面窗口的设备上下文
user32.ReleaseDC(hwnd, hdc_src)
continue
else:
break
print(hwnd)
print(hdc_src)
if len(str(hdc_src)) > 16:
return
s = time.time()
# 创建一个与屏幕兼容的内存设备上下文
hdc_dest = gdi32.CreateCompatibleDC(hdc_src)
# 创建一个位图
bmp = gdi32.CreateCompatibleBitmap(hdc_src, width, height)
# 将位图选入内存设备上下文
old_bmp = gdi32.SelectObject(hdc_dest, bmp)
# 定义SRCCOPY常量
SRCCOPY = 0x00CC0020
# 捕获屏幕
gdi32.BitBlt(hdc_dest, 0, 0, width, height, hdc_src, x, y, SRCCOPY)
"""
gdi32.BitBlt(hdc_src, # 目标设备上下文
x_dest, # 目标矩形左上角的x坐标
y_dest, # 目标矩形左上角的y坐标
width, # 宽度
height, # 高度
hdc_dest, # 源设备上下文
x_src, # 源矩形左上角的x坐标(通常是0)
y_src, # 源矩形左上角的y坐标(通常是0)
SRCCOPY) # 复制选项
"""
# 定义 RGBQUAD 结构体
class RGBQUAD(ctypes.Structure):
_fields_ = [("rgbBlue", ctypes.c_ubyte),
("rgbGreen", ctypes.c_ubyte),
("rgbRed", ctypes.c_ubyte),
("rgbReserved", ctypes.c_ubyte)]
# 定义 BITMAPINFOHEADER 结构体
class BITMAPINFOHEADER(ctypes.Structure):
_fields_ = [("biSize", ctypes.c_uint),
("biWidth", ctypes.c_int),
("biHeight", ctypes.c_int),
("biPlanes", ctypes.c_ushort),
("biBitCount", ctypes.c_ushort),
("biCompression", ctypes.c_uint),
("biSizeImage", ctypes.c_uint),
("biXPelsPerMeter", ctypes.c_int),
("biYPelsPerMeter", ctypes.c_int),
("biClrUsed", ctypes.c_uint),
("biClrImportant", ctypes.c_uint)]
# 定义 BITMAPINFO 结构体
class BITMAPINFO(ctypes.Structure):
_fields_ = [("bmiHeader", BITMAPINFOHEADER),
("bmiColors", RGBQUAD * 3)] # 只分配了3个RGBQUAD的空间
BI_RGB = 0
DIB_RGB_COLORS = 0
# 分配像素数据缓冲区(这里以24位位图为例,每个像素3字节)
pixel_data = (ctypes.c_ubyte * (width * height * 3))() # 4
# 转换bytes
# pixel_data = memoryview(pixel_data).tobytes()
# 填充 BITMAPINFO 结构体
bmi = BITMAPINFO()
bmi.bmiHeader.biSize = ctypes.sizeof(BITMAPINFOHEADER)
bmi.bmiHeader.biWidth = width
bmi.bmiHeader.biHeight = height # 注意:没有负的是位图
bmi.bmiHeader.biPlanes = 1
bmi.bmiHeader.biBitCount = 24 # 24即3*8 32
bmi.bmiHeader.biCompression = BI_RGB # 无压缩
# 调用 GetDIBits 获取像素数据
ret = gdi32.GetDIBits(hdc_src, bmp, 0, height, pixel_data, ctypes.byref(bmi), DIB_RGB_COLORS)
if ret == 0:
print("GetDIBits failed:", ctypes.WinError())
print(time.time() - s)
# 像素
mv = (memoryview(pixel_data).cast('B'))
# 位图标记
s1 = b"BM"
# 文件大小 纯粹的字节
s2 = len(pixel_data) + 54
# 保留字段1
s3 = 0
# 保留字段2
s4 = 0
# 像素数据偏移量
s5 = 54
start_sign = struct.pack("<2sIHHI", s1, s2, s3, s4, s5)
print(start_sign)
# 信息头大小
n1 = 40
# 图像宽度
n2 = screenWidth
# 图像高度
n3 = screenHeight
# 平面数
n4 = 1
# 颜色深度
n5 = 24
# 压缩方式
n6 = 0
# 图像大小
n7 = len(pixel_data)
# 水平分辨率
n8 = 0
# 垂直分辨率
n9 = 0
# 使用的颜色数
n10 = 0
# 重要颜色数
n11 = 0
print(n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11)
end_sign = struct.pack('
import io
import time
import struct
import ctypes
s = time.time()
gdi32 = ctypes.windll.gdi32
user32 = ctypes.windll.user32
# 定义常量
SM_CXSCREEN = 0
SM_CYSCREEN = 1
# 缩放比例
zoom = 1
screenWidth = int(user32.GetSystemMetrics(SM_CXSCREEN) * zoom)
screenHeight = int(user32.GetSystemMetrics(SM_CYSCREEN) * zoom)
# 屏幕截取
def capture_screen(x, y, width, height):
for _ in range(100):
# time.sleep(0.01)
# 获取桌面窗口句柄
hwnd = user32.GetDesktopWindow()
# 获取桌面窗口的设备上下文
hdc_src = user32.GetDC(hwnd)
if len(str(hdc_src)) > 16:
# time.sleep(0.01)
# 释放桌面窗口的设备上下文
user32.ReleaseDC(hwnd, hdc_src)
continue
else:
break
print(hwnd)
print(hdc_src)
if len(str(hdc_src)) > 16:
return
s = time.time()
# 创建一个与屏幕兼容的内存设备上下文
hdc_dest = gdi32.CreateCompatibleDC(hdc_src)
# 创建一个位图
bmp = gdi32.CreateCompatibleBitmap(hdc_src, width, height)
# 将位图选入内存设备上下文
old_bmp = gdi32.SelectObject(hdc_dest, bmp)
# 定义SRCCOPY常量
SRCCOPY = 0x00CC0020
# 捕获屏幕
gdi32.BitBlt(hdc_dest, 0, 0, width, height, hdc_src, x, y, SRCCOPY)
"""
gdi32.BitBlt(hdc_src, # 目标设备上下文
x_dest, # 目标矩形左上角的x坐标
y_dest, # 目标矩形左上角的y坐标
width, # 宽度
height, # 高度
hdc_dest, # 源设备上下文
x_src, # 源矩形左上角的x坐标(通常是0)
y_src, # 源矩形左上角的y坐标(通常是0)
SRCCOPY) # 复制选项
"""
# 定义 RGBQUAD 结构体
class RGBQUAD(ctypes.Structure):
_fields_ = [("rgbBlue", ctypes.c_ubyte),
("rgbGreen", ctypes.c_ubyte),
("rgbRed", ctypes.c_ubyte),
("rgbReserved", ctypes.c_ubyte)]
# 定义 BITMAPINFOHEADER 结构体
class BITMAPINFOHEADER(ctypes.Structure):
_fields_ = [("biSize", ctypes.c_uint),
("biWidth", ctypes.c_int),
("biHeight", ctypes.c_int),
("biPlanes", ctypes.c_ushort),
("biBitCount", ctypes.c_ushort),
("biCompression", ctypes.c_uint),
("biSizeImage", ctypes.c_uint),
("biXPelsPerMeter", ctypes.c_int),
("biYPelsPerMeter", ctypes.c_int),
("biClrUsed", ctypes.c_uint),
("biClrImportant", ctypes.c_uint)]
# 定义 BITMAPINFO 结构体
class BITMAPINFO(ctypes.Structure):
_fields_ = [("bmiHeader", BITMAPINFOHEADER),
("bmiColors", RGBQUAD * 3)] # 只分配了3个RGBQUAD的空间
BI_RGB = 0
DIB_RGB_COLORS = 0
# 分配像素数据缓冲区(这里以24位位图为例,每个像素3字节)
pixel_data = (ctypes.c_ubyte * (width * height * 3))() # 4
# 转换bytes
# pixel_data = memoryview(pixel_data).tobytes()
# 填充 BITMAPINFO 结构体
bmi = BITMAPINFO()
bmi.bmiHeader.biSize = ctypes.sizeof(BITMAPINFOHEADER)
bmi.bmiHeader.biWidth = width
bmi.bmiHeader.biHeight = height # 注意:没有负的是位图
bmi.bmiHeader.biPlanes = 1
bmi.bmiHeader.biBitCount = 24 # 24即3*8 32
bmi.bmiHeader.biCompression = BI_RGB # 无压缩
# 调用 GetDIBits 获取像素数据
ret = gdi32.GetDIBits(hdc_src, bmp, 0, height, pixel_data, ctypes.byref(bmi), DIB_RGB_COLORS)
if ret == 0:
print("GetDIBits failed:", ctypes.WinError())
print(time.time() - s)
# 像素
mv = (memoryview(pixel_data).cast('B'))
# 位图标记
s1 = b"BM"
# 文件大小 纯粹的字节
s2 = len(pixel_data) + 54
# 保留字段1
s3 = 0
# 保留字段2
s4 = 0
# 像素数据偏移量
s5 = 54
start_sign = struct.pack("<2sIHHI", s1, s2, s3, s4, s5)
print(start_sign)
# 信息头大小
n1 = 40
# 图像宽度
n2 = screenWidth
# 图像高度
n3 = screenHeight
# 平面数
n4 = 1
# 颜色深度
n5 = 24
# 压缩方式
n6 = 0
# 图像大小
n7 = len(pixel_data)
# 水平分辨率
n8 = 0
# 垂直分辨率
n9 = 0
# 使用的颜色数
n10 = 0
# 重要颜色数
n11 = 0
print(n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11)
end_sign = struct.pack('