Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does the get_image Method Support Windows? #8

Closed
whwanyt opened this issue Mar 11, 2024 · 12 comments · Fixed by #6
Closed

Does the get_image Method Support Windows? #8

whwanyt opened this issue Mar 11, 2024 · 12 comments · Fixed by #6
Labels
bug Something isn't working

Comments

@whwanyt
Copy link

whwanyt commented Mar 11, 2024

Hello,

I'm experiencing an issue with the clipboard_rs crate on Windows 10, where I'm unable to retrieve an image from the clipboard using the provided example code. Despite having used the clipboard image tool to copy an image to the clipboard, the result returned by get_image is an empty image.

Here is the code snippet I'm using:

use clipboard_rs::{common::RustImage, Clipboard, ClipboardContext};
let ctx = ClipboardContext::new().unwrap();
let img = ctx.get_image().unwrap();
@ChurchTao
Copy link
Owner

@whwanyt
Sorry, what I am testing here is the screenshot scene. What is your scene? Copy image files directly?

@ChurchTao
Copy link
Owner

Maybe I am not compatible with all image formats, you can provide

  • Before get_image(), print available_formats()
  • How do you copy the picture?

@whwanyt
Copy link
Author

whwanyt commented Mar 11, 2024

@ChurchTao I will reply to the code result at night because I am not in front of the computer right now.

@ChurchTao ChurchTao added the question Further information is requested label Mar 11, 2024
@ChurchTao
Copy link
Owner

@whwanyt
OK, I'm waiting for your news.

@ChurchTao
Copy link
Owner

ChurchTao commented Mar 11, 2024

I may have guessed your problem. If you use the image.rs demo to run, you need to change the file saving path to the path in your own operating system.
Like

const TMP_PATH: &str = "C:\\Windows\\Temp\\test.png";

I optimized the demo code so that it can run directly in Windows. You can take a look.

8058263#diff-7bf759a09f1216d26074fca12a9e8a2aac8ca2d0392e34ed84fa6974d988aa9bR3

@whwanyt
Copy link
Author

whwanyt commented Mar 11, 2024

I used the following sample code to get the clipboard picture and convert it to a base64 character string:

use base64::{engine::general_purpose, Engine as _};
use clipboard_rs::{common::RustImage, Clipboard, ClipboardContext};
fn main() {
    let ctx = ClipboardContext::new().unwrap();
    let image = ctx.get_image().unwrap();
    match image.to_png() {
        Ok(res) => {
            let image_bytes = res.get_bytes();
            let base64_str = general_purpose::STANDARD_NO_PAD.encode(&image_bytes);
            println!("{}", base64_str);
        }
        Err(e) => {
            println!("err: {}", e)
        }
    }
}

The output is as follows:

err: image is empty

I use electron _ screenshot this plugin to achieve screenshots to the clipboard

@whwanyt
Copy link
Author

whwanyt commented Mar 11, 2024

I may have guessed your problem. If you use the image.rs demo to run, you need to change the file saving path to the path in your own operating system. Like

const TMP_PATH: &str = "C:\\Windows\\Temp\\test.png";

I optimized the demo code so that it can run directly in Windows. You can take a look.

8058263#diff-7bf759a09f1216d26074fca12a9e8a2aac8ca2d0392e34ed84fa6974d988aa9bR3

Thank you for your reply, my goal is to get the image of the screen captured using the plugin by get_image method and convert it to base64 format

@ChurchTao
Copy link
Owner

@whwanyt
Before calling get_image, print available_formats to show me.

@whwanyt
Copy link
Author

whwanyt commented Mar 11, 2024

@whwanyt Before calling get_image, print available_formats to show me.

use clipboard_rs::{Clipboard, ClipboardContext};
fn main() {
    let ctx = ClipboardContext::new().unwrap();
    let types = ctx.available_formats().unwrap();
    println!("{:?}", types);
}

The print type results are as follows

    Finished dev [unoptimized + debuginfo] target(s) in 0.06s
     Running `target\debug\imgdemo.exe`
["CF_BITMAP", "CF_DIB", "CF_DIBV5"]

@ChurchTao
Copy link
Owner

@whwanyt
I understand. Sure enough, I am not compatible with images in bitmap format. I will fix it in the next version.

@ChurchTao ChurchTao added bug Something isn't working and removed question Further information is requested labels Mar 11, 2024
@whwanyt whwanyt changed the title [Question] Does the get_image Method Support Windows? Does the get_image Method Support Windows? Mar 11, 2024
@ChurchTao
Copy link
Owner

ChurchTao commented Mar 13, 2024

@whwanyt
Good news, I have solved the compatibility problem. Can you please try the code of this new branch again?
https://github.com/ChurchTao/clipboard-rs/tree/dev-watcher-api-change

@ChurchTao ChurchTao linked a pull request Mar 14, 2024 that will close this issue
@ChurchTao
Copy link
Owner

The new version has been released. I will close this issue for now. If there are any problems, just reopen it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants