Skip to content

Instantly share code, notes, and snippets.

@HuakunShen
Created January 8, 2023 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HuakunShen/fa9f97870165ea19ea621bedcbdefb13 to your computer and use it in GitHub Desktop.
Save HuakunShen/fa9f97870165ea19ea621bedcbdefb13 to your computer and use it in GitHub Desktop.
Read Clipboard Image and Write to a File using rust
// cargo add image
// cargo add arboard
use std::convert::TryInto;
use arboard::*;
use image::*;
fn main() {
let mut clipboard = Clipboard::new().unwrap();
let image = match clipboard.get_image() {
Ok(img) => img,
Err(e) => {
eprintln!("error getting image: {}", e);
return;
}
};
eprintln!("getting {}x{} image", image.width, image.height);
let image: RgbaImage = ImageBuffer::from_raw(
image.width.try_into().unwrap(),
image.height.try_into().unwrap(),
image.bytes.into_owned(),
)
.unwrap();
let image = DynamicImage::ImageRgba8(image);
image.save("rust-clipboard.png").unwrap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment