How to create a GtkImage from a Cairo context?

Posted by Zurbo82 on Stack Overflow See other posts from Stack Overflow or by Zurbo82
Published on 2010-12-23T18:32:06Z Indexed on 2010/12/23 19:54 UTC
Read the original article Hit count: 220

Filed under:
|

I got a paint function that works using a Cairo context and the end result should be a GtkImage (without intermediate image creation). I tried to use the gdk_cairo_create function but this code:

...
GdkPixbuf *pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, 22, 22);
GtkWidget *image = gtk_image_new_from_pixbuf (pixbuf);
GdkDrawable *drawable = image->window;
cairo_t *ctx = gdk_cairo_create (drawable);
my_cairo_paint_function (ctx);
...

fails with:

Gdk-CRITICAL **: IA__gdk_cairo_create: assertion `GDK_IS_DRAWABLE (drawable)' failed

Same with a simple:

#include <gtk/gtk.h>
#include <cairo.h>

int
main (int argc, char *argv[])
{
  gtk_init(&argc, &argv);
  cairo_t *ctx = gdk_cairo_create (gtk_widget_get_window (gtk_image_new_from_file ("foobar.png")));
  gtk_main();
  return 0;
}

I don't understand why this fails. Any help is appreciated!

© Stack Overflow or respective owner

Related posts about gtk

Related posts about cairo