Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw an image on drawing area

Tags:

c

gtk

drawing

I am trying to draw an image on a drawing area with no luck.I saw a couple of python examples but I was not able to implement them into c ,which I am using (eg draw an image to gtk.DrawingArea?)

I have already created a Pixbuf variable to store the image I want to draw on the drawing area,but there are no functions like gtk_drawing_area_draw_pixbuf or something related to that.Any suggestion is appreciated.

like image 552
Rrjrjtlokrthjji Avatar asked Dec 01 '22 22:12

Rrjrjtlokrthjji


1 Answers

You need to make use of expose-event callback (assuming you are working with Gtk+ 2.0) to draw the pixbuf onto drawing area. There is no gtk_drawing_area_draw_pixbuf instead you have gdk_draw_pixbuf. This has been deprecated in favour of gdk_cairo_set_source_pixbuf from version 2.22 onwards. You can call these function in your expose event callback something on these lines (please use them as reference only):
If your Gtk version is < 2.22

static gboolean
da_expose (GtkWidget *da, GdkEvent *event, gpointer data)
{
    (void)event; (void)data;
    GdkPixbuf *pix;
    GError *err = NULL;
    /* Create pixbuf */
    pix = gdk_pixbuf_new_from_file("/usr/share/icons/cab_view.png", &err);
    if(err)
    {
        printf("Error : %s\n", err->message);
        g_error_free(err);
        return FALSE;
    }
    GdkDrawable *draw = gtk_widget_get_window(da);
    /* Draw pixbuf */
    gdk_draw_pixbuf(draw, NULL, pix, 0, 0, 0, 0, -1, -1, GDK_RGB_DITHER_NONE, 0, 0);
    return FALSE;
}

Version 2.22 onwards you will have to make use of cairo something on these lines:

static gboolean
da_expose (GtkWidget *da, GdkEvent *event, gpointer data)
{
    (void)event; (void)data;
    GdkPixbuf *pix;
    GError *err = NULL;
    /* Create pixbuf */
    pix = gdk_pixbuf_new_from_file("/usr/share/icons/cab_view.png", &err);
    if(err)
    {
        printf("Error : %s\n", err->message);
        g_error_free(err);
        return FALSE;
    }
    cairo_t *cr;
    cr = gdk_cairo_create (da->window);
    gdk_cairo_set_source_pixbuf(cr, pix, 0, 0);
    cairo_paint(cr);
    cairo_fill (cr);
    cairo_destroy (cr);
    return FALSE;
}

Of course you would have connected to the callback using g_signal_connect (say g_signal_connect (da, "expose-event", G_CALLBACK (da_expose), NULL);). If you are using Gtk+ 3.0 then you will be making use of draw instead of expose-event. You can always refer to gtk-demo/gtk3-demo application which are available to see the samples along with the code. This should be available in the package repository of your distro or you can always get it from source.
Hope this helps!
PS: This link might provide you with some pointers

like image 107
another.anon.coward Avatar answered Dec 19 '22 09:12

another.anon.coward