How to get a screenshot of a specific window with Rust on Windows?
I would like to know how to take screenshots of a part of a specific window. There may be an overlay on top of the application (a game overlay) that hides what I'm interested in. I would like to find a way to take a screenshot of the application only, ignoring the overlay or what would be on top.
And I wonder if it's possible to optimize it in order to have ~5 screenshots/seconds
For now i tried the screenshots cargo package with the following code :
use opencv::{core, highgui, imgcodecs};
use screenshots::Screen;
use std::{time::Instant};
use opencv::core::{log, Mat};
const WIDTH: i32 = 275;
const HEIGHT: i32 = 275;
fn get_img(screen: Screen) -> Mat {
let image = screen.capture().unwrap();
let buffer: Vec<u8> = image.into();
// Change image type to OpenCV Mat
let original_image: Mat = imgcodecs::imdecode(&core::Mat::from_slice(buffer.as_slice()).unwrap(), imgcodecs::IMREAD_COLOR).unwrap();
return original_image;
}
fn main() {
let window_name = "test".to_owned();
highgui::named_window(&window_name, highgui::WINDOW_NORMAL).unwrap();
highgui::resize_window(&window_name, WIDTH, HEIGHT).unwrap();
let screens = Screen::all().unwrap();
let screen = screens[1].clone();
let mut img = get_img(screen);
loop {
let now = Instant::now();
img = get_img(screen);
// print in console the time it took to process the image
println!("{} ms", now.elapsed().as_millis());
}
}
But it seem not possible to take screenshot of just a specific window behind an overlay.
I use
cargo run --release
The target os is Windows and I'm also developing under Windows.
ps : I convert my image to OpenCV Mat for the next part of my code
Comments
Post a Comment