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 self.__hwnd
@hwnd.setter
def hwnd(self, hwnd):
if not hwnd or self.hwnd == hwnd:
return
self.__hwnd = hwnd
self.dxgi = self.create_dxgi(hwnd)
def __del__(self):
if self.dxgi:
self.dxgi.destroy()
def __call__(self, hwnd):
self.hwnd = hwnd
shotLeft, shotTop, width, height = self.getWindowRect()
shot = np.ndarray((height, width, 4), dtype=np.uint8)
shotPointer = shot.ctypes.data_as(POINTER(c_ubyte))
buffer = self.dxgi.grab(shotPointer, shotLeft, shotTop, width, height)
image = np.ctypeslib.as_array(buffer, shape=(height, width, 4))
image = cv2.cvtColor(image, cv2.COLOR_BGRA2RGB)
return image
def getWindowRect(self):
left, top, right, bottom = win32gui.GetWindowRect(self.hwnd)
shotLeft, shotTop = 0, 0
height = bottom - top
width = right - left
return shotLeft, shotTop, width, height
def create_dxgi(self, hwnd):
if getattr(self, "dxgi", None):
self.dxgi.destroy()
dxgi = ctypes.CDLL(str(root / "dxgi4py.dll"))
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)
return dxgi
调用实例
获取目标窗口句柄后,即可初始化截图器进行单次截图。返回的是 RGB 格式的 numpy.ndarray,可直接用于 OpenCV 显示或模型推理。
import win32gui
from dxgi_capture import DxgiCapture
# 获取目标窗口句柄(例如:查找原神窗口)
hwnd = win32gui.FindWindow(None, "原神")
# 初始化截图器
capture = DxgiCapture()
capture.hwnd = hwnd
# 单次截图
frame = capture(hwnd)
# 返回 RGB 格式的 numpy.ndarray (H, W, 3)
# 可直接用于 OpenCV 显示或模型推理
import cv2
cv2.imshow("Game Capture", frame)
cv2.waitKey(0)
首次使用需安装依赖库:
pip install pywin32 opencv-python numpy
技术原理简述
dxgi4py.dll 封装了以下关键步骤:
- 通过 IDXGIFactory1 枚举显卡适配器;
- 创建 IDXGIOutputDuplication 对象,绑定到目标窗口所在的显示器;
- 使用 AcquireNextFrame 获取 GPU 帧缓冲区;
- 将显存数据拷贝到 CPU 内存,并转换为 BGRA 格式;
- 通过 ctypes 指针传递给 Python,构建 NumPy 数组。
整个过程绕过 GDI,直接与 DirectX 交互,因此能捕获受保护内容。
常见问题
Q:是否支持 Vulkan 游戏? A:部分支持。Vulkan 内容若通过 DXGI 共享(如 Steam Overlay 开启),可被捕获;否则需额外层(如 OBS 的 Vulkan 钩子)。
Q:能否截取 UWP 应用(如 Xbox Game Bar)? A:受限于 Windows 安全策略,部分 UWP 应用无法捕获。
Q:是否需要管理员权限? A:不需要!普通用户权限即可运行。
结语
如果你正在开发游戏辅助工具、AI 自动化脚本或直播监控系统,这套基于 DXGI 的截图方案将极大提升你的效率与稳定性。相比老旧的 BitBlt 方法,DXGI 方案在处理现代图形接口时具有显著的性能优势。

