[cairo] how to cropping or clipping an image using cairo library

Carl Worth cworth at cworth.org
Sun Jan 27 22:02:23 PST 2008


On Mon, 28 Jan 2008 10:58:21 +0530 (IST), anurag swarnkar wrote:
> I want to know whether there is any API available in cairo through
> which i can crop or clip (getting a portion of image) an image?

Yes.

The simplest case, (which I'm guess is what you're wanting), is just a
rectangle, but doing fancier stuff isn't actually any harder. I'll
show both.

For example, if you have the image in a cairo surface named "image"
and you are drawing to some cairo context "cr" then you might do
something like this, (let's assume that we want the origin of the
complete image at (x0,y0) and that we want to display only a
rectangular portion of the image (x,y)-(width,height)). So that would
look like:

	cairo_set_source_surface (cr, image, x0, y0);
	cairo_rectangle (cr, x0 + x, y0 + y, width, height);
	cairo_fill (cr);

And if you wanted to crop out a circular portion of the image, you
might instead do:

	cairo_set_source_surface (cr, image, x0, y0);
	cairo_arc (cr, x0 + width / 2, y0 + height / 2,
		   MIN (WIDTH,HEIGHT) / 2, 0, 2 * M_PI);
	cairo_fill (cr);

Or if you wanted to crop out any polygonal region in points (a,b),
(c,d)...:

	cairo_set_source_surface (cr, image, x0, y0);
	cairo_move_to (cr, a, b);
	cairo_line_to (cr, c, d);
	...
	cairo_fill (cr);

Anyway, hopefully that gives you the basic idea. And of course, you
can "crop out" text-shaped portions of an image with
cairo_set_source_surface;cairo_show_text as well.

Have fun with cairo!

-Carl
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://lists.cairographics.org/archives/cairo/attachments/20080127/cb8e5d1c/attachment.pgp 


More information about the cairo mailing list