Skip to content

Show image from "raw" bytes (or DynamicImage) #3431

Answered by JumpyLionnn
Bastl34 asked this question in Q&A
Discussion options

You must be logged in to vote

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.

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(),
          )

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@pozix604
Comment options

Answer selected by Bastl34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants