Rust

Capturing Full Screen with screenshots #Rust

This article introduces how to capture screenshots with Rust. We'll capture the entire screen and save images to a specified folder.

Shou Arisaka
2 min read
Nov 5, 2025

This article introduces how to capture screenshots with Rust. We’ll capture the entire screen and save images to a specified folder.

To capture screenshots with Rust, it’s common to use a crate called screenshots - crates.io: Rust Package Registry.

Capturing Screenshots with Rust

First, add the following to Cargo.toml. This allows you to use the screenshots crate.

screenshots = "0.8.6"

The overall code looks like this. This defines a function called screenshot() that captures a screenshot.


use screenshots::Screen;
use std::time::Instant;

// screenshot(dest) -> void
fn screenshot(dest: &str) {
    let start = Instant::now();
    let screens = Screen::all().unwrap();

    for screen in screens {
        println!("capturer {screen:?}");
        let mut image = screen.capture().unwrap();
        image
            .save(format!("{}", dest))
            .unwrap();
    }
    println!("screenshot took {}ms", start.elapsed().as_millis());
}

The execution flow of this function is as follows:

  1. use screenshots::Screen;: Enable use of the Screen struct from the screenshots crate
  2. use std::time::Instant;: Enable use of the Instant struct from std’s time
  3. fn screenshot(dest: &str) {: Define a function called screenshot. The argument is a string type called dest
  4. let start = Instant::now();: Assign the current time to a variable called start
  5. let screens = Screen::all().unwrap();: Assign all screens to a variable called screens
  6. for screen in screens {: Assign the contents of screens to a variable called screen and loop
  7. let mut image = screen.capture().unwrap();: Capture a screenshot of screen and assign it to a variable called image
  8. image.save(format!("{}", dest)).unwrap();: Save image with the filename dest
  9. println!("screenshot took {}ms", start.elapsed().as_millis());: Display the time it took to capture the screenshot

That’s all.

This function can be called as follows:

screenshot("screenshot.png");

Or it can also be called as follows:

screenshot("./path/to/screenshot.png");

Summary

This article introduced how to capture screenshots with Rust.

Share this article

Shou Arisaka Nov 5, 2025

🔗 Copy Links