Rust 屏幕截图

180it 2023-05-15 PM 2998℃ 0条

截屏
https://github.com/nashaofu/screenshots-rs

添加依赖

[dependencies]
screenshots = "0.5.3"

use screenshots::Screen;
use std::{fs, time::Instant};

fn main() {
    let mut start;
    let screens = Screen::all().unwrap();
    fs::create_dir_all("D:/Test/").unwrap();
    for screen in screens {
        if screen.display_info.is_primary {
            start = Instant::now();
            println!("屏幕: {screen:?}");
            let image = screen.capture().unwrap();
            let buffer = image.buffer();
            fs::write(format!("D:/Test/{}.png", screen.display_info.id), buffer).unwrap();
            println!("运行耗时: {:?}", start.elapsed());
        }
    }

    start = Instant::now();
    // 获取点所在屏幕
    let screen = Screen::from_point(100, 100).unwrap();
    println!("点所在屏幕: {screen:?}");
    let image = screen.capture_area(300, 300, 300, 300).unwrap();
    let buffer = image.buffer();
    fs::write("D:/Test/capture_display_with_point.png", buffer).unwrap();
    println!("运行耗时: {:?}", start.elapsed());
}
支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

Rust 屏幕截图