Created
January 8, 2023 16:57
-
-
Save HuakunShen/fa9f97870165ea19ea621bedcbdefb13 to your computer and use it in GitHub Desktop.
Read Clipboard Image and Write to a File using rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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