Transferring an image from rust to slint

The image is taken from the file and transferred to slint. this code works.

let somstr = "znachki/".to_string() + &pic1 + "@2x.png";   
   let image_path = Path::new(&somstr);
   let image_pic = image::open(&image_path).expect("Fualed to open image file"); 
     
  let buffer = SharedPixelBuffer::<Rgba8Pixel>::clone_from_slice( 
    image_pic.as_rgba8().expect("Fault image to buffer!"),
    image_pic.width(),
    image_pic.height()
appwin.set_picname(Image::from_rgba8(buffer));

how to do the same thing, but take the picture from the site
how can I copy the resulting image to the clipboard?

let urlstring =  "http://openweathermap.org/img/wn/".to_string() + &pic1 + "@2x.png";
let pic = reqwest::blocking::get(&urlstring).unwrap();

let buffer = SharedPixelBuffer::<Rgba8Pixel>::clone_from_slice( 
    pic.as_rgba8().expect("Fault image to buffer!"),
    pic.width(),
    pic.height()

Instead of using the blocking API of request, you can use the async API wrapped in slint::spawn_local.
But becasue reqwest uses the tokio runtime, it needs to be wrapped with async_compat

So it'll look something like that:

slint::spawn_local(async_compat::Compat::new(async move {
    let urlstring =  "http://openweathermap.org/img/wn/".to_string() + &pic1 + "@2x.png";
    let body = reqwest::get(&urlstring)
    .await.unwrap()
    .bytes()
    .await.unwrap();
   let image_pic = image::load_from_memory(&body).unwrap();
     let buffer = SharedPixelBuffer::<Rgba8Pixel>::clone_from_slice( 
    image_pic.as_rgba8().expect("Fault image to buffer!"),
    image_pic.width(),
    image_pic.height())
  appwin.set_picname(Image::from_rgba8(buffer));
   
})).unwrap()

(untested)

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.