Skip to content

How to copy rendered image from texture_id to clipboard? #407

@elgar328

Description

@elgar328

Hi! I'm working on a Bevy application using bevy_egui and I'm trying to implement a feature to copy the rendered 3D preview image to the clipboard.
I have a setup similar to the render_to_image_widget.rs example where I render a 3D scene to a texture and display it in an egui widget using texture_id. The image displays correctly in the UI, but I need to copy it to the clipboard.
Currently, I can get the texture_id from contexts.image_id(&render_image), but I need to convert this to an egui::ColorImage to use ui.ctx().copy_image(). However, I can't find a way to read the pixel data from the texture_id back to CPU memory.
Here's what I'm trying to do:

// This works - displays the image correctly
let texture_id = contexts.image_id(&render_image).unwrap();
ui.painter().image(
    texture_id,
    rect,
    egui::Rect::from_min_max(egui::pos2(0.0, 0.0), egui::pos2(1.0, 1.0)),
    egui::Color32::WHITE,
);

// This is what I need - but how do I get the image data?
if ui.button("Copy to Clipboard").clicked() {
    // How do I convert texture_id to ColorImage?
    let color_image = /* convert texture_id to ColorImage */;
    ui.ctx().copy_image(color_image);
}

The issue is that the rendered image is in GPU memory, but egui::ColorImage needs to be in CPU memory. Is there a built-in way in bevy_egui to read back the texture data, or do I need to use Bevy's GPU readback system separately?
I've tried using Bevy's GpuReadbackPlugin and Readback component, but it's quite complex and I'm wondering if there's a simpler approach through bevy_egui.
Any guidance would be greatly appreciated! Thanks in advance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions