Create GtkImage from URL

I’m trying to load an image from an URL and then apply it to a GTKImage, however, I’m running under some issues.

            let url = element.attributes.get("src").unwrap().clone().unwrap();

            let handle = thread::spawn(move || {
                let result = reqwest::blocking::get(url).unwrap().bytes().unwrap();

            let img_data = handle.join().unwrap();

            let img_stream = gio::MemoryInputStream::from_bytes(&Bytes::from(&img_data));

            let stream =
                gdk_pixbuf::Pixbuf::from_stream(&img_stream, Some(&gio::Cancellable::new()))

            let image = gtk::Image::builder()


My problem is that the normal width & height of the image (112x140 in this case) don’t correctly apply (because the image has “GTK_ICON_SIZE_INHERIT”, and icon_size(...) only accepts an enum with 3 values: Inherit, Small, Large). I resulted into setting the image semi-properly using pixel_size() of width(), but that means it won’t properly check for the height.

Images below for reference


Testing, but without modifying pixel_size (the default behavior, and where the problem is):

And the confusing part is that the width() and height() on the PixBuf is correct, it’s the way GTKImage “renders it” that probably makes it small.

I’m confused - what’s the proper way of doing this?

gtk::Image is meant to be used for icons and other assets where the size of the asset determines the size of the widget; for random images, use gtk::Picture instead.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.