The Screenshot struct provides several ways to capture visual data from the Windows desktop or specific UI elements.
Capture Methods
Screenshot::capture_desktop(): Captures the entire virtual screen, including all connected monitors. It handles negative coordinates for multi-monitor setups.Screenshot::capture_rect(rect: Rect): Captures a specific rectangular region defined by a Rect. Coordinates are in screen pixels and support negative values for secondary displays.Screenshot::capture_element(element: &UIElement): A convenience method that retrieves the bounding rectangle of a UIElement and captures only that area.
Pixel Formats
By default, captured data uses the PixelFormat::BGRA format, which is raw GDI output (4 bytes per pixel, bottom-up row order). To use the data with most modern image libraries, convert it to top-down RGBA using to_rgba().
Saving Screenshots
save_bmp(path): Saves the screenshot as a BMP file.save_png(path): Saves the screenshot as a PNG file. Note: This requires the png feature to be enabled in your Cargo.toml. It automatically converts the image to RGBA before encoding.
use uiautomation::screenshots::Screenshot;
use uiautomation::types::Rect;
// Capture a specific area
let rect = Rect::new(0, 0, 800, 600);
let screenshot = Screenshot::capture_rect(rect)?;
// Save as BMP
screenshot.save_bmp("screenshot.bmp")?;
// Or convert to RGBA for processing
let rgba_screenshot = screenshot.to_rgba();
println!("Width: {}, Height: {}", rgba_screenshot.width(), rgba_screenshot.height());