From 2bb6d82f555206aafb5e9eb7c4c3f12058af75aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Aedo?= Date: Sat, 29 Aug 2020 20:48:00 -0400 Subject: [PATCH] Added --to-clipboard to windows --- Cargo.toml | 1 + src/bin.rs | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 65878bc..ba04edc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,6 +31,7 @@ pasteboard = "0.1.1" [target.'cfg(target_os = "windows")'.dependencies] clipboard-win = "4.0.2" +image = { version = "0.23", default-features = false, features = ["jpeg", "bmp", "jpeg_rayon", "png"] } [dependencies.image] version = "0.23" diff --git a/src/bin.rs b/src/bin.rs index 852c988..80c4660 100644 --- a/src/bin.rs +++ b/src/bin.rs @@ -12,7 +12,7 @@ use syntect::easy::HighlightLines; use syntect::util::LinesWithEndings; #[cfg(target_os = "windows")] use { - clipboard_win::{formats::RawData, set_clipboard}, + clipboard_win::{formats, Clipboard, Setter}, image::ImageOutputFormat, }; #[cfg(target_os = "macos")] @@ -57,9 +57,19 @@ pub fn dump_image_to_clipboard(image: &DynamicImage) -> Result<(), Error> { #[cfg(target_os = "windows")] pub fn dump_image_to_clipboard(image: &DynamicImage) -> Result<(), Error> { let mut temp: Vec = Vec::new(); - image.write_to(&mut temp, ImageOutputFormat::Png)?; - set_clipboard(RawData(49488), &temp) - .map_err(|e| format_err!("Failed to access clipboard {}", e))?; + + // Convert the image to RGB without alpha because the clipboard + // of windows doesn't support it. + let image = DynamicImage::ImageRgb8(image.to_rgb()); + + image.write_to(&mut temp, ImageOutputFormat::Bmp)?; + + let _clip = + Clipboard::new_attempts(10).map_err(|e| format_err!("Couldn't open clipboard: {}", e)); + + formats::Bitmap + .write_clipboard(&temp) + .map_err(|e| format_err!("Failed copy image: {}", e)); Ok(()) }