Show image from "raw" bytes (or DynamicImage) #3431
-
How can I show an image which is already present as RGBA-data? I have tried something like this but without success: let pixels = image.as_bytes().to_vec(); // where image is of type DynamicImage (image crate)
let image = egui::Image::from_bytes("bytes://", pixels);
ui.add(image); I'm getting errors that the loader was not found. |
Beta Was this translation helpful? Give feedback.
Answered by
JumpyLionnn
Oct 24, 2023
Replies: 1 comment 1 reply
-
First, you need to make sure the image is in the rgba8 format (or rgb). After that you need to convert it to a use image::EncodableLayout;
let color_image = match &image {
DynamicImage::ImageRgb8(image) => {
// common case optimization
egui::ColorImage::from_rgb(
[image.width() as usize, image.height() as usize],
image.as_bytes(),
)
},
other => {
let image = other.to_rgba8();
egui::ColorImage::from_rgba_unmultiplied(
[image.width() as usize, image.height() as usize],
image.as_bytes(),
)
},
};
// you must keep the handle, if the handle is destroyed so the texture will be destroyed as well
let handle = ctx.load_texture(name, color_image, options);
let sized_image = egui::load::SizedTexture::new(handle.id(), egui::vec2(color_image.size[0] as f32, color_image.size[1] as f32));
let image = egui::Image::from_texture(sized_image); I hope i helped :) |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
Bastl34
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
First, you need to make sure the image is in the rgba8 format (or rgb). After that you need to convert it to a
ColorImage
.using the
ColorImage
you can load the texture in the context.