use image::RgbImage;
use screencap::{CaptureType, Monitor, PixelFormat, Region};
use std::io;
use std::path::Path;
fn main() -> io::Result<()> {
let list = Monitor::all()?;
for x in list {
println!("{x:?},{:?}", x.size())
}
let monitor = Monitor::primary()?;
let mut grabber = screencap::ScreenGrabber::new(&monitor, CaptureType::default())?;
std::thread::sleep(std::time::Duration::from_millis(100));
let (width, height) = monitor.size()?;
let mut buf = vec![0; (width * height * 4) as usize];
let width = width / 2;
let height = height / 2;
let region = Region {
left: 0,
top: 0,
width,
height,
};
let (len, width, height) =
grabber.next_frame_region_format(&mut buf, Some(region), PixelFormat::RGB)?;
println!("next_frame_region_format {:?},{:?}", width, height);
let image =
RgbImage::from_raw(width, height, buf[..len].to_vec()).expect("Failed to create image");
let path = Path::new("screenshot.jpg");
image.save(path).expect("Failed to save image");
Ok(())
}