#screen-capture #screenshot #window #windows #capture #screen

win-screenshot

在 Windows 平台上对特定窗口或整个屏幕进行截图

26 个版本 (稳定)

4.0.11 2024 年 6 月 20 日
4.0.8 2024 年 3 月 27 日
4.0.6 2023 年 11 月 28 日
4.0.4 2023 年 5 月 29 日
0.1.6 2022 年 6 月 3 日

#417 in GUI

Download history 22/week @ 2024-05-05 9/week @ 2024-05-19 6/week @ 2024-05-26 21/week @ 2024-06-02 24/week @ 2024-06-09 125/week @ 2024-06-16 67/week @ 2024-06-23 35/week @ 2024-06-30 63/week @ 2024-07-07 27/week @ 2024-07-14 31/week @ 2024-07-21 26/week @ 2024-07-28 22/week @ 2024-08-04 57/week @ 2024-08-11 49/week @ 2024-08-18

每月下载量 154
libserpix_rs 中使用

MIT/Apache

23KB
506 代码行

win-screenshot

在 Windows 平台上对特定窗口或整个屏幕进行截图

已知问题

capture_window() 为某些窗口绘制黑色边框
如果您调用 capture_window() 并得到 0x80070578 "无效窗口句柄",请确保捕获的窗口没有被最小化

最低要求

capture_window() 使用未记录的 PW_RENDERFULLCONTENT,该功能首次出现在 Windows 8.1 中

示例

use image::{DynamicImage, RgbaImage};
use regex::Regex;
use win_screenshot::prelude::*;

fn main() {
    // Capture entire screen
    let buf = capture_display().unwrap();

    // Capture window by known id
    let buf = capture_window(11996706).unwrap();

    // Capture window if you know the exact name
    let hwnd = find_window("Notepad").unwrap();
    let buf = capture_window(hwnd).unwrap();

    // If you don't know the exact name, try to find it
    let re = Regex::new(r"Steam").unwrap();
    let hwnd = window_list()
        .unwrap()
        .iter()
        .find(|i| re.is_match(&i.window_name))
        .unwrap()
        .hwnd;
    let buf = capture_window(hwnd).unwrap();

    // convert to image and save
    let img = DynamicImage::ImageRgba8(
        RgbaImage::from_raw(buf.width, buf.height, buf.pixels).unwrap());
    img.to_rgb8().save("screenshot.jpg").unwrap();

    // Fine tuning

    // BitBlt dramatically faster, often fails
    // (e.g. firefox, steam, 3d accelerated windows)
    let using = Using::BitBlt;
    // PrintWindow much slower, much more reliable
    let using = Using::PrintWindow;

    // Capture client area of window
    let area = Area::ClientOnly;
    // Capture whole window (not supported with BitBlt)
    let area = Area::Full;

    // Build-in crop, faster on large windows
    let crop_xy = None; //Some([100, 100]);
    let crop_wh = None; //Some([300, 300]);
    let buf = capture_window_ex(hwnd, using, area, crop_xy, crop_wh).unwrap();
}

依赖项

~128MB
~2M SLoC