我正在尝试在gtk-rs中的DrawingArea
上渲染图像(gif / png)。我可以使用Pixbuf
:
Pixbuf::new_from_file("/path/to/img.gif")
但我找不到将Pixbuf
呈现为cairo::Context
的方法。我注意到gdk::prelude::ContextExt
有set_source_pixbuf()
:
https://docs.rs/crate/gdk/0.1.4/source/src/cairo_interaction.rs
所以我试着用这个:
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);
});
但没有渲染。 ContextExt
似乎未实现(它似乎为gdk_cairo_set_source_pixbuf
的第二个参数指定了null)?
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);
}
}
是否还有其他方法可以在DrawingArea
上呈现图片?
答案 0 :(得分:1)
我需要使用Context.paint()
代替Context.stroke()
:
context.set_source_pixbuf(&ws.pix, 0f64, 0f64);
context.paint(); // need to call paint() instead of stroke().
return Inhibit(false);