Luca Bruno blog

Create a cairo surface from a pixbuf

Hello,
sometimes in the code of a couple of projects I see some hard algorithms to transform a pixbuf in a cairo surface.
Maybe most of people don’t know that GdkCairoContext, contrarily to cairo_t, is created against a cairo context not a cairo surface:

`surface = cairo.ImageSurface (cairo.FORMAT_ARGB32, pixbuf.get_width(), pixbuf.get_height())

cr = cairo.Context (surface)

gdkcr = gtk.gdk.CairoContext (cr)

gdkcr.set_source_pixbuf (pixbuf)

gdkcr.paint ()`

Notice gtk.gdk.CairoContext (cr), which cr is not the surface. That’s the key of the code snippet.

For instance this can be applied on a ClutterCairoTexture to render pixbufs in the canvas.