Python 基于 DXGI 实现现代游戏窗口无闪烁截图方案
在 Windows 平台上,很多现代游戏(尤其是使用 DirectX 11/12 或 Vulkan 渲染的游戏)会启用硬件加速和独占全屏模式。传统的截图方法往往无法捕获这些窗口的内容,结果要么是黑屏,要么是桌面背景。
这是因为游戏画面直接由 GPU 渲染到显存,不经过 GDI;操作系统出于性能和安全考虑,限制了普通程序对受保护窗口的访问。
那么,有没有一种能在后台稳定、高效、无闪烁地截取游戏窗口的方法?答案是:有!使用 DXGI(DirectX Graphics Infrastructure)技术。
解决方案:基于 DXGI 的 Python 封装类
我们开发了一个轻量级 Python 类 DxgiCapture,它通过调用一个原生 C++ 编写的 DLL(dxgi4py.dll),利用 Windows 的 DXGI Desktop Duplication API 实现对任意窗口(包括全屏/无边框游戏)的高速截图。
核心优势
- 支持 DX11 / DX12 游戏(如《原神》《CS2》《永劫无间》《艾尔登法环》等)
- 无需前台激活窗口,真正后台运行
- 帧率高、延迟低,适合自动化脚本、AI 训练、直播监控等场景
- 返回 NumPy 数组,无缝对接 OpenCV / PyTorch / TensorFlow
核心代码
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()
self.user32.SetProcessDpiAwarenessContext()
@property
def hwnd(self):
return self.__hwnd
@hwnd.setter
def hwnd(self, hwnd):
hwnd .hwnd == hwnd:
.__hwnd = hwnd
.dxgi = .create_dxgi(hwnd)
():
.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

