[cairo] How to output images from data originating from files?

Simon Sapin simon.sapin at exyr.org
Fri Apr 5 07:13:46 PDT 2013

Le 05/04/2013 16:03, Ken Resander a écrit :
> I am new to Cairo and have fairly limited knowlege of graphics (Windows
> GDI,GDI+ years ago).
> I am now on Ubuntu 12.04 and need to render images in png and jpg from
> GTK in C/C++.
> I know how to do this when specifying .png and .jpg file names:
> cairo_surface_t * imgpng =
> cairo_image_surface_create_from_png("sample_7.png");
> cairo_set_source_surface(cr, imgpng, 0,40);
> cairo_paint(cr);
> GError * err = NULL;
> GdkPixbuf * imgpxb = gdk_pixbuf_new_from_file("parrots.jpg",&err);
> gdk_cairo_set_source_pixbuf(cr, imgpxb, 0, 150);
> cairo_paint (cr);
> My program will receive image data from another computer via standard
> protocols. There might be hundreds of small to small-medium images
> coming, so it would more efficient to batch the file content of these
> into a huge buffer and pass that across (from the other computer) along
> with the image format, buffer offset and length of each chunk.
> Of course I could copy a chunk into a tmp file and use the png surface
> and gdkpixbuf filename api calls above, but it seems such a shame having
> to put the data back into a file for cairo/gdk to read and convert it to
> the format needed.
> Is there a better way?


If you have images (not all PNG) in memory, the easiest way to load them 
without going through temporary files is to use GdkPixbufLoader:


This will give you GdkPixbuf objects that you can convert to cairo 
patterns with gdk_cairo_set_source_pixbuf, as you do above.

Simon Sapin

More information about the cairo mailing list