[cairo] How do I use an X11 Pixmap in a GtkImage?

adlo adloconwy at gmail.com
Wed Aug 5 06:36:03 PDT 2015

I am writing a program that gets previews of open windows and puts them into a GtkImage, like this:

XCompositeRedirectWindow (dpy, xid, CompositeRedirectAutomatic);
Pixmap pixmap = XCompositeNameWindowPixmap (dpy, xid);

GdkPixmap *gdk_pixmap = gdk_pixmap_new (NULL, width/2, height/2, 24);
cairo_t *cr = gdk_cairo_create (gdk_pixmap);

cairo_surface_t *s = cairo_xlib_surface_create (dpy, pixmap, visual, width, height);

cairo_scale (cr, 0.5, 0.5);
cairo_rectangle (cr, 0, 0, width, height);
cairo_set_source_surface (cr, s, 0, 0);
cairo_fill (cr);

GtkImage *image = gtk_image_new_from_pixmap (gdk_pixmap, NULL);

I have two problems:

My widgets have a 24-bit depth, therefore my GdkPixmap needs to also have a 24-bit depth, but some of my window previews have a 32-bit depth. How can I convert my previews to 24-bit depth?

If I attempt to scale my previews below half their size using cairo_scale, they fail to draw at all. How can I resolve this?



More information about the cairo mailing list