Skip to content

Commit

Permalink
Added --to-clipboard to windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastián Aedo committed Aug 30, 2020
1 parent cb4708e commit 2bb6d82
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
18 changes: 14 additions & 4 deletions src/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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<u8> = 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(())
}

Expand Down

0 comments on commit 2bb6d82

Please sign in to comment.