[cairo] Need small help

Carl Worth cworth at east.isi.edu
Tue Feb 24 05:22:59 PST 2004

On Feb 20, Ravindra wrote:
 > I'm new to Cairo. I would appreciate if someone could answer my
 > following two questions,

No problem at all. Questions like this are useful for identifying
holes in the current documentation and demos.

 > 1. How to draw an existing image on a new image that I would be creating
 > using cairo_t? Existing image could be in a file.

3 easy steps:

	1) Load the image (use something besides cairo such as libpng)

	2) Create a cairo_surface_t for the image, (using
           cairo_surface_create_for_image, oddly enough)

	3) Display by image by calling cairo_show_surface, (it will be
           displayed at the current point --- set with cairo_move_to).

The tutorial slides that Keith put together:


demonstrates complete code for this process. See "Loading an Image
File" and "Image Example". 

 > 2. Is it possible to have two cairo_t structures in my program and
 > create an image using one struct and later draw this image on the other
 > cairo_t struct (also destroying the first struct)?

Yes, that should all work fine. You may create a cairo_surface_t of
any type, then draw to it using cairo after calling
cairo_set_target_surface. Then, you may use that surface as the source
image for later calls to cairo_show_surface using a different cairo_t.

 > My guess is to use cairo_set_pattern (cairo_surface_t), but I'm not very
 > sure of its proper usage. Please provide some pointers in the right
 > direction. In the meantime, I would be looking at cairo-demo samples.

Using cairo_set_pattern will also work. Unlike cairo_show_surface,
cairo_set_pattern is not a painting function, so nothing will appear
immediately after this call. Instead, cairo_set_pattern will cause the
given surface to be used as the "paint" for subsequent calls to
cairo_stroke or cairo_fill. Again, see Keith's slides for example code
and resulting images.

I hope this helps,


More information about the cairo mailing list