[cairo] How to convert cairo content to gdk-bifbuf

Sven Herzberg herzi-ml at gnome-de.org
Sat Jul 15 11:32:46 PDT 2006


On Fr, 2006-07-14 at 10:52 -0700, Li Li wrote:
> My question is how to convert the cairo drawing context to gdk-pixbuf.
> 
> I tried to use following codes, but not works.

Dude, your just missing ONE single thing:

Skip this one:
> =================================================================
>    data = (guchar*)malloc(4*width*height*sizeof(guchar));
> 
>    cairo_surface_t *cs= cairo_image_surface_create_for_data(data,
>             CAIRO_FORMAT_RGB24, width, height, 4*width);
> 
>    cairo_t *cr=cairo_create(cs);

And write:

GdkPixbuf* pix = gdk_pixbuf_new...(...); // update to size/image format
                                         // etc. as you want to
cairo_t* cr = gdk_cairo_create(pix);

>    cairo_rectangle (cr, 0, 0, width, height);
> 
>    cairo_clip (cr);
> 
>    snippet1(cr);  //draw something onto it

And finish with:
cairo_destroy(cr);

>    cr_convert_to_pixbuf(data, width, height, pixbuf);//do conversion
> =================================================================


Done, that should work, as gdk_cairo_create() does this for GdkDrawable
which is realized by GdkWindow and GdkPixbuf.

Regards,
  Sven



More information about the cairo mailing list