image.rs
1 #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release 2 3 use eframe::egui; 4 use egui_extras::RetainedImage; 5 6 fn main() { 7 let options = eframe::NativeOptions { 8 initial_window_size: Some(egui::vec2(500.0, 900.0)), 9 ..Default::default() 10 }; 11 eframe::run_native( 12 "Show an image with eframe/egui", 13 options, 14 Box::new(|_cc| Box::new(MyApp::default())), 15 ); 16 } 17 18 struct MyApp { 19 image: RetainedImage, 20 } 21 22 impl Default for MyApp { 23 fn default() -> Self { 24 Self { 25 image: RetainedImage::from_image_bytes( 26 "rust-logo-256x256.png", 27 include_bytes!("rust-logo-256x256.png"), 28 ) 29 .unwrap(), 30 } 31 } 32 } 33 34 impl eframe::App for MyApp { 35 fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { 36 egui::CentralPanel::default().show(ctx, |ui| { 37 ui.heading("This is an image:"); 38 self.image.show(ui); 39 40 ui.heading("This is a rotated image:"); 41 ui.add( 42 egui::Image::new(self.image.texture_id(ctx), self.image.size_vec2()) 43 .rotate(45.0_f32.to_radians(), egui::Vec2::splat(0.5)), 44 ); 45 46 ui.heading("This is an image you can click:"); 47 ui.add(egui::ImageButton::new( 48 self.image.texture_id(ctx), 49 self.image.size_vec2(), 50 )); 51 }); 52 } 53 }