Windows 新版本怎么实现截图 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
xueyuehua
V2EX    Python

Windows 新版本怎么实现截图

  •  
  •   xueyuehua 275 天前 1987 次点击
    这是一个创建于 275 天前的主题,其中的信息可能已经有所发展或是发生改变。

    以前写过一个 Python 脚本,用 pywin32 利用窗口句柄实现内存截图,在 win10 上可以正常使用,窗口被遮挡也可以截图,甚至可以在后台操作鼠标。 但是为什么在 win11 上用不了了,用 c# 实现也是不行,是 win11 新内核导致的吗? 有大佬知道什么解决方案吗

    4 条回复    2025-01-08 16:50:52 +08:00
    b4115Z4mRAz9P8EA
        1
    b4115Z4mRAz9P8EA  
       275 天前   1
    Windows 11 确实对窗口安全性和隐私做了一些增强,这影响了一些基于窗口句柄的操作。以下是几种可能的解决方案:
    1 使用 Windows Graphics Capture API:
    ```
    # 需要安装 Windows SDK 和 Python 包
    # pip install windows-capture
    from windows_capture import WindowsGraphicsCapture

    def capture_window(hwnd):
    capturer = WindowsGraphicsCapture()
    image = capturer.capture_window(hwnd)
    return image
    ```

    2 使用提升权限的方式:
    ```
    import win32security
    import win32con
    import win32api

    def enable_privileges():
    # 获取需要的权限
    priv_flags = (
    win32security.TOKEN_ADJUST_PRIVILEGES |
    win32security.TOKEN_QUERY
    )

    # 获取当前进程的 token
    h_token = win32security.OpenProcessToken(
    win32api.GetCurrentProcess(),
    priv_flags
    )

    # 提升权限
    privileges = [
    (win32security.LookupPrivilegeValue(
    None,
    win32security.SE_DEBUG_NAME
    ),
    win32con.SE_PRIVILEGE_ENABLED)
    ]

    # 应用权限
    win32security.AdjustTokenPrivileges(
    h_token,
    0,
    privileges
    )
    ```

    3 使用 DirectX 捕获方式:
    ```
    // C# 示例
    using SharpDX;
    using SharpDX.Direct3D11;
    using SharpDX.DXGI;

    public class ScreenCapture
    {
    private Factory1 factory;
    private Adapter1 adapter;
    private Device device;
    private Output output;
    private Output1 output1;

    public void Initialize()
    {
    factory = new Factory1();
    adapter = factory.GetAdapter1(0);
    device = new Device(adapter);
    output = adapter.GetOutput(0);
    output1 = output.QueryInterface<Output1>();
    }

    public Bitmap CaptureScreen()
    {
    // 实现截图逻辑
    // ...
    }
    }
    ```

    4 使用 SetWinEventHook 监听窗口事件:
    ```
    import win32con
    import win32api
    import win32gui
    import ctypes
    from ctypes import wintypes

    def win_event_callback(hWinEventHook, event, hwnd, idObject, idChild, dwEventThread, dwmsEventTime):
    if event == win32con.EVENT_SYSTEM_FOREGROUND:
    # 处理窗口事件
    pass

    def setup_hook():
    user32 = ctypes.windll.user32

    # 设置事件钩子
    WinEventProcType = ctypes.WINFUNCTYPE(
    None,
    wintypes.HANDLE,
    wintypes.DWORD,
    wintypes.HWND,
    wintypes.LONG,
    wintypes.LONG,
    wintypes.DWORD,
    wintypes.DWORD
    )

    win_evet_proc = WinEventProcType(win_event_callback)

    user32.SetWinEventHook(
    win32con.EVENT_SYSTEM_FOREGROUND,
    win32con.EVENT_SYSTEM_FOREGROUND,
    0,
    win_event_proc,
    0,
    0,
    win32con.WINEVENT_OUTOFCONTEXT
    )
    ```

    5 考虑使用 UI 自动化框架:
    ```
    from comtypes.client import CreateObject

    def capture_using_uiautomation():
    UIAutomation = CreateObject("UIAutomationCore.CUIAutomation")
    element = UIAutomation.GetRootElement()
    # 实现截图和自动化操作
    ```

    主要建议:
    1 优先使用 Windows Graphics Capture API ,这是微软推荐的新方法
    2 如果需要后台操作,考虑使用 UI 自动化框架
    3 对于特权操作,确保:
    以管理员权限运行
    正确处理 UAC
    申请必要的系统权限

    4 如果是开发生产环境应用,建议:
    使用官方支持的 API
    实现适当的错误处理
    考虑兼容性问题

    5 监控系统事件和性能:
    使用事件钩子
    实现错误日志
    处理资源释放

    这些方法中,Windows Graphics Capture API 是最推荐的方案,因为它:
    是官方支持的新 API
    性能更好
    兼容性更好
    更安全
    如果这些方法都不能满足需求,可能需要考虑使用其他技术方案或重新设计应用程序的架构。
    xueyuehua
        2
    xueyuehua  
    OP
       275 天前
    @yqf0215 谢谢大佬给了这么多种方案,我试试看
    xqzr
        3
    xqzr  
       275 天前   1
    @Livid #1 AI(?)
    b4115Z4mRAz9P8EA
        4
    b4115Z4mRAz9P8EA  
       274 天前
    cursor
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     3941 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 26ms UTC 04:11 PVG 12:11 LAX 21:11 JFK 00:11
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86