Artificial truth

The more you see, the less you believe.

[archives] [latest] | [homepage] | [atom/rss]

Embed image into a gtk application
Thu 11 April 2013 — download

When you want to distribute a binary without depending of external files, you may want to embed them. In GTK, there is no (unlike in Qt) elegant way to do this. But, you can use the dirty one : converting your image into a variable.

First, convert your image:

gdk-pixbuf-csource --raw --name=myimage_inline myimage.png > myimage.h

Next, include your newly created header into your app, and build your GtkImage using

#include "myimage.h"
...
GdkPixbuf* pixbuf = gdk_pixbuf_new_from_inline (-1, myimage_inline, FALSE, NULL);
GtkWidget* logo = gtk_image_new_from_pixbuf(pixbuf);

Since we are sure about our image source and size, we can disable length checking (-1) and ignore errors (FALSE).

Source: The GTK documentation