Get ImageSource from TextureHandle

Any hint on how to get ImageSource from TextureHandle . The issue came along while compiling a crate with updated dependecies.

    pub fn draw(&mut self, ui: &mut Ui) {
        let texture: &mut TextureHandle = self.tex_handle.get_or_insert_with(|| {
            // Load the texture only once.
                .load_texture("my-image", self.render_image.clone(), TextureOptions::LINEAR)

            .update_result(&mut self.render_image);

        texture.set(self.render_image.clone(), TextureOptions::LINEAR);
        ui.vertical(|ui| {
            if self.renderer.waiting_for_next_frame {
                let pb = ProgressBar::new(self.renderer.progress as f32)
            ui.image(texture, ui.available_size()); // COMPILATION error here

You have to give more details, such as:

  • The crate(s) that you are using
  • The compiler error
1 Like

The following crates have been updated to the recent version


egui = { version = "0.25.0" }

eframe = { version = "0.25.0" }

rand = "0.8.5"

And since then , I have the compilation error

error[E0277]: the trait bound SizedTexture: From<&mut TextureHandle> is not satisfied
--> src\app\
52 | ui.image(texture);
| ----- ^^^^^^^ the trait From<&mut TextureHandle> is not implemented for SizedTexture
| |
| required by a bound introduced by this call
= help: the following other types implement trait From<T>:
<SizedTexture as From<&'a TextureHandle>>
<SizedTexture as From<(TextureId, egui::Vec2)>>
= note: required for &mut TextureHandle to implement Into<SizedTexture>
= note: required for ImageSource<'_> to implement From<&mut TextureHandle>
= note: 1 redundant requirement hidden
= note: required for &mut TextureHandle to implement Into<ImageSource<'_>>
note: required by a bound in Ui::image
--> C:\Users\a295697.cargo\registry\src\\egui-0.25.0\src\
1576 | pub fn image<'a>(&mut self, source: impl Into<ImageSource<'a>>) -> Response {
| ^^^^^^^^^^^^^^^^^^^^^ required by this bound in Ui::image

Can you try passing the texture handle as a shared reference?

1 Like

(Jinx, posting for the links)


ui.image(&*texture, ui.available_size());


1 Like

It does not help . The recent version does not accept two input arguments, it accepts only one. Probably you could download the crate from here and try recompile using the latest dependencies as I mentioned above.

Ui::image takes a source: impl Into<ImageSource<'a>> argument, which means that source can be anything that implements Into<ImageSource<'a>>, which means "anything that can be converted into an ImageSource".

ImageSource is an enum

pub enum ImageSource<'a> {
    Uri(Cow<'a, str>),
    Bytes {
        uri: Cow<'static, str>,
        bytes: Bytes,

SizedTexture sounds like what we're after, since currently what's being passed is a texture and a size. A SizedTexture can be constructed with SizedTexture::new and implements Into<ImageSource<'a>>[1], so we can pass it to Ui::image as the source argument.

Something like

let texture = SizedTexture::new(texture, ui.available_size());

should hopefully work.

In the future, it's best to list all the relevant crates clearly from the start, it's not obvious from the first post that this is related to egui, raytracer-rs or any other crate in particular.

  1. This is a little convoluted, but impl<T: Into<SizedTexture>> From<T> for ImageSource<'static> essentially makes it so all types that can be converted into a SizedTexture can also be converted into an ImageSource. SizedTexture itself can be "converted" into a SizedTexture, so a SizedTexture can also be converted into an ImageSource thanks to this impl... ↩ī¸Ž


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.