Skip to content

How to maintain aspect ratio when showing background image? #5335

Answered by marci1175
Crispy13 asked this question in Q&A
Discussion options

You must be logged in to vote

Hi!
I am not entirely sure why maintain_aspect_ratio doesn't work.
It is possible that egui doesn't know the size of the image. You can easily check if you call Image::size on img.

I could work around this issue by using the image crate (Which is a requirement if you want to load images in egui, so you don't have to add a new dependency), and a few manual calculations.

//Get available rect
let available_rect = ui.ctx().available_rect();
//Fetch available size
let available_size = available_rect.size();
//Opcaity
let opacity: u8 = 10;

//Loading the image bytes (You can use any other way you would like)
let img_src = include_bytes!("../image.jpg");

//Load image into memory
let img_mem = i…

Replies: 1 comment 1 reply

Comment options

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

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