Lonami Lonami - 4 months ago 9x
Linux Question

Embed images in Vala project for later use in UI


I'm new to Vala development (although I have some years of experience with C#) under Linux, and I decided to recreate one of my C# programs, however, I need to use images in the UI.

My problem

How can I embed resource files (such as images) for later use in the UI? How to access them later? And how to put them in a button?

What I'm using

I'm using Linux (Mint) and Anjuta development IDE, with the Glade UI designer integrated. The Vala project targets a GTK+ 3.0 project.

What I've tried

I have tried adding a new specific objective for images, adding them into the project... But I don't seem to succeed.
I have seen the default images provided by Linux, and those work just fine, but I need to add my own.

Thanks in advance!


The normal approach would be to install your images to PREFIX/share/pixmaps/YOUR_APP. For example Gnumeric installs some .png files in /usr/share/pixmaps/gnumeric/.

You can use GResource to embed binary files (like images) into your executable if you really want to. The glib-compile-resources command can be added to your build system (see also this question).

You can also use icons from the users icon theme.

You didn't write what component you want to use to display your images, so I'll assume Gtk.Image here.

Gtk.Image has several constructors for the purpose of loading the image:

  • from_icon_name loads the image from the current icon theme (which is useful to support user themes).
  • from_resource loads the image from an embedded GResource.
  • from_file loads the image from a file.

See the main documentation of Gtk.Image for more methods. Some other widgets have similar methods to load images (for example toolbar buttons).

You should not use from_stock any more (There was a stock system in Gtk+ that is being replaced by freedesktop.org icon schemas).