[cairo] create an image from window for printing

Kalle Vahlman kalle.vahlman at gmail.com
Sat Jan 20 02:59:47 PST 2007


2007/1/19, Fabian Förg <fabian.foerg at gmx.de>:
> Hello,
>
> is it possible to capture a gtk-window, save it as an image file and use
> that file for printing with cairo?
> At first, I tried this:
> pixmap = gdk_pixmap_new(window->window, window->allocation.width,
> window->allocation.height, -1);
> This should save the window as a pixmap.

Actually it doesn't. It just creates a pixmap that has the same depth
that the gdkwindow does. The content of that pixmap is undefined at
that point, if I'm not mistaken.

> In order to print it, I tried gdk_cairo_set_source_pixbuf, but the
> function requires a pixbuf as an argument.
> How can I convert the pixmap to a pixbuf and is my approach alright?

First, you can get a pixbuf directly from the window with the function

  gdk_pixbuf_get_from_drawable()

Secondly, I haven't tried the printing code of Gtk+ yet myself
(assuming that is what you are using), but I gather from these
questions and the API docs that it offers you a cairo context to which
you draw your stuff for printing.

So rather than converting the window to a pixbuf in between, you are
probably better off geting a cairo surface for the window and simply
painting that to the context you get from the printing system. The Gdk
Cairo helpers don't offer it directly, but you can get it by aquiring
the context and asking the target surface from it:

  cairo_context = gdk_cairo_create(window->window);
  surface = cairo_get_target(cairo_context);

and then you can set the surface as the source for the printing context:

  cairo_set_source_surface (printing_context, surface);

and do whatever transforms and tricks you want to with it (fitting it
to the page size or such) before painting it to the surface.

Cheers,

-- 
Kalle Vahlman, zuh at iki.fi
Powered by http://movial.fi
Interesting stuff at http://syslog.movial.fi


More information about the cairo mailing list