Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render an image on cairo::Context in gtk-rs

Tags:

rust

gtk

I'm trying to render an image (gif/png) on a DrawingArea in gtk-rs. I can read the image file with Pixbuf:

Pixbuf::new_from_file("/path/to/img.gif")

But I cannot find a way to render the Pixbuf into cairo::Context. I noticed that gdk::prelude::ContextExt has set_source_pixbuf():

https://docs.rs/crate/gdk/0.1.4/source/src/cairo_interaction.rs

So I tried to use this:

extern crate gdk;
use gdk::prelude::*;

...

drawingArea.connect_draw(move |widget, context| {
    context.set_source_pixbuf(&ws.pix, 0f64, 0f64);
    context.stroke();
    return Inhibit(false);
});

But nothing is rendered. The ContextExt seems unimplemented (it seems to specify null to the second parameter of gdk_cairo_set_source_pixbuf)?

fn set_source_pixbuf(&self, pixbuf: &Pixbuf, x: f64, y: f64) {
    unsafe {
        ffi::gdk_cairo_set_source_pixbuf(self.to_glib_none().0, pixbuf.to_glib_none().0, x, y);
    }
}

Is there any other method to render an image on DrawingArea?

like image 615
ruimo Avatar asked Dec 04 '25 10:12

ruimo


1 Answers

I need to use Context.paint() instead of Context.stroke():

context.set_source_pixbuf(&ws.pix, 0f64, 0f64);
context.paint();  // need to call paint() instead of stroke().
return Inhibit(false);
like image 151
ruimo Avatar answered Dec 07 '25 17:12

ruimo