You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用mss库截图报错:mss.exception.ScreenShotError: gdi32.GetDIBits() failed

解决mss截图时的gdi32.GetDIBits() failed错误

问题代码

...
ScreenSizeX = 1600
ScreenSizeY = 900
with mss.mss() as sct: 
    monitor = {'top': 0, 'left': 0, 'width': ScreenSizeX, 'height': ScreenSizeY}

while True:
    img = np.array(sct.grab(monitor))
    ...

报错信息

Traceback (most recent call last): File "D:\Users\totha\Downloads\Leonardo-main\Aimbot.py", line 20, in <module> img = np.array(sct.grab(monitor)) ^^^^^^^^^^^^^^^^^ File "D:\Python 3.11\Lib\site-packages\mss\base.py", line 90, in grab screenshot = self._grab_impl(monitor) ^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\Python 3.11\Lib\site-packages\mss\windows.py", line 252, in _grab_impl raise ScreenShotError("gdi32.GetDIBits() failed.") mss.exception.ScreenShotError: gdi32.GetDIBits() failed.

修复方案

  • 匹配真实屏幕分辨率:别硬编码分辨率,直接用mss获取主显示器参数,避免手动设置错误:

    with mss.mss() as sct:
        # monitors[1]对应主显示器,monitors[0]是所有显示器的总区域
        monitor = sct.monitors[1]
    
  • 以管理员身份运行脚本:Windows下部分窗口或程序需要管理员权限才能被捕获,右键点击Python脚本或终端,选择“以管理员身份运行”。

  • 关闭硬件加速:部分显卡的硬件加速会干扰GDI截图,关闭浏览器、游戏等程序的硬件加速选项,或者在显卡控制面板里调整相关设置。

  • 更新依赖和驱动:执行pip install --upgrade mss更新mss库到最新版;同时更新显卡驱动,修复底层API的兼容问题。

  • 缩小截图区域:如果不需要全屏截图,先尝试截取小范围区域,排查是否是特定区域导致的报错。

内容的提问来源于stack exchange,提问作者rcong

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 11:05:04