image/
image.rs

1#[cfg(target_os = "linux")]
2use clipboard_rs::ClipboardContextX11Options;
3use clipboard_rs::{common::RustImage, Clipboard, ClipboardContext};
4
5#[cfg(target_os = "macos")]
6const TMP_PATH: &str = "/tmp/";
7#[cfg(target_os = "windows")]
8const TMP_PATH: &str = "C:\\Windows\\Temp\\";
9#[cfg(all(
10	unix,
11	not(any(
12		target_os = "macos",
13		target_os = "ios",
14		target_os = "android",
15		target_os = "emscripten"
16	))
17))]
18const TMP_PATH: &str = "/tmp/";
19
20#[cfg(target_os = "linux")]
21fn setup_clipboard() -> ClipboardContext {
22	ClipboardContext::new_with_options(ClipboardContextX11Options { read_timeout: None }).unwrap()
23}
24
25#[cfg(not(target_os = "linux"))]
26fn setup_clipboard() -> ClipboardContext {
27	ClipboardContext::new().unwrap()
28}
29
30fn main() {
31	let ctx = setup_clipboard();
32
33	let types = ctx.available_formats().unwrap();
34	println!("{:?}", types);
35
36	let img = ctx.get_image();
37
38	match img {
39		Ok(img) => {
40			let _ = img
41				.save_to_path(format!("{}test.png", TMP_PATH).as_str())
42				.map_err(|e| println!("save test.png err={}", e));
43
44			let resize_img = img
45				.thumbnail(300, 300)
46				.map_err(|e| println!("thumbnail err={}", e))
47				.unwrap();
48
49			let _ = resize_img
50				.save_to_path(format!("{}test_thumbnail.png", TMP_PATH).as_str())
51				.map_err(|e| println!("save test_thumbnail.png err={}", e));
52		}
53		Err(err) => {
54			println!("err={}", err);
55		}
56	}
57}