nashaofu / xcap

XCap is a cross-platform screen capture library written in Rust. It supports Linux (X11, Wayland), MacOS, and Windows. XCap supports screenshot and video recording (to be implemented).

Home Page:https://docs.rs/xcap

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Area size is invalid when capture from current window.

ihsanrabb opened this issue · comments

Halo, i try to capture from current active window in rust. I use active_win_pos_rs to track position from current active window and capture using capture_area, but somehow if the current window is not primary I always get error Area size is invalid. I try read screenshots-rs code to try understand the error but still don't understand fully. can you help with this?

use screenshots::{Screen, Compression};
use std::{fs};
use active_win_pos_rs::get_active_window;

#[tauri::command]
pub async fn screenshot_window(filename: &str) -> Result<(), ()> {  
  match get_active_window() {
    Ok(active_window) => {
      let mut _x: i32 = active_window.position.x as i32;
      let mut _y: i32 = active_window.position.y as i32;
      let _width: u32 = active_window.position.width as u32;
      let _height: u32 = active_window.position.height as u32;

      let screen = Screen::from_point(_x,_y).unwrap();

      let image = screen.capture_area(_x, _y, _width, _height).unwrap();
      let buffer = image.to_png(Compression::Fast).unwrap();
    },
    Err(()) => {
      println!("error occurred while getting the active window");
    }
  }

  Ok(())
}

In the case of a single display, and after maximizing the window, the obtained window position is as follows: WindowPosition { x: -8.0, y: -8.0, width: 2576.0, height: 1408.0 } }

use active_win_pos_rs::get_active_window;
use screenshots::Screen;

pub fn main() -> Result<(), ()> {
    match get_active_window() {
        Ok(active_window) => {
            println!("active_window {:?}", active_window);
            println!("screens {:?}", Screen::all().unwrap());

            let mut _x: i32 = active_window.position.x as i32;
            let mut _y: i32 = active_window.position.y as i32;
            let _width: u32 = active_window.position.width as u32;
            let _height: u32 = active_window.position.height as u32;

            let screen = Screen::from_point(_x, _y).unwrap();


            let image = screen.capture_area(_x, _y, _width, _height).unwrap();
            image.save("./a.png").unwrap();
        }
        Err(()) => {
            println!("error occurred while getting the active window");
        }
    }

    Ok(())
}

do you have any suggestion for this minus window position in single display and after maximizing the window? @nashaofu

#91 window capture is WIP,this may be helpful to you @ihsanrabb