Python 实现现代游戏窗口无闪烁、高性能 DX11/DX12 截图方案
在 Windows 平台上,许多现代游戏(尤其是使用 DirectX 11/12 或 Vulkan 渲染的游戏)启用了硬件加速和独占全屏模式。传统的截图方法如 PIL.ImageGrab 或 pyautogui.screenshot() 往往无法捕获这些窗口的内容,结果要么是黑屏,要么是桌面背景。
这是因为游戏画面直接由 GPU 渲染到显存,不经过 GDI,且操作系统出于性能和安全考虑限制了普通程序对受保护窗口的访问。要解决这个问题,可以使用 DXGI(DirectX Graphics Infrastructure)技术,特别是 DXGI Desktop Duplication API。
解决方案:基于 DXGI 的 Python 封装类
我们开发了一个轻量级 Python 类 DxgiCapture,它通过调用原生 C++ 编写的 DLL(dxgi4py.dll),利用 Windows 的 DXGI Desktop Duplication API 实现对任意窗口(包括全屏/无边框游戏)的高速截图。
核心优势包括支持 DX11/DX12 游戏、无需前台激活窗口真正后台运行、帧率高延迟低适合自动化脚本与 AI 训练场景,以及返回 NumPy 数组无缝对接 OpenCV 或 PyTorch。
核心代码实现
这里展示 DxgiCapture 类的关键实现逻辑。为了处理 DPI 感知问题,我们在初始化时设置了进程 DPI 上下文。代码中使用了 ctypes 来加载本地 DLL 并传递指针给 Python 层构建 NumPy 数组。
import ctypes
from ctypes import *
import numpy as np
import win32gui
import cv2
from pathlib import Path
root = Path(__file__).parent
class DxgiCapture:
def __init__(self):
self.dxgi = None
self.__hwnd = None
self.user32 = ctypes.windll.user32
self.user32.SetProcessDPIAware()
try:
# 设置每显示器 DPI 感知,避免缩放问题
self.user32.SetProcessDpiAwarenessContext(-3)
except Exception:
pass
@property
def hwnd(self):
return .__hwnd
():
hwnd .hwnd == hwnd:
.__hwnd = hwnd
.dxgi = .create_dxgi(hwnd)
():
.dxgi:
.dxgi.destroy()
():
.hwnd = hwnd
shotLeft, shotTop, width, height = .getWindowRect()
shot = np.ndarray((height, width, ), dtype=np.uint8)
shotPointer = shot.ctypes.data_as(POINTER(c_ubyte))
buffer = .dxgi.grab(shotPointer, shotLeft, shotTop, width, height)
image = np.ctypeslib.as_array(buffer, shape=(height, width, ))
image = cv2.cvtColor(image, cv2.COLOR_BGRA2RGB)
image
():
left, top, right, bottom = win32gui.GetWindowRect(.hwnd)
shotLeft, shotTop = ,
height = bottom - top
width = right - left
shotLeft, shotTop, width, height
():
(, , ):
.dxgi.destroy()
dxgi = ctypes.CDLL((root / ))
dxgi.grab.argtypes = (POINTER(ctypes.c_ubyte), ctypes.c_int, c_int, c_int, c_int,)
dxgi.grab.restype = POINTER(c_ubyte)
dxgi.init_dxgi(hwnd)
dxgi

