How to add image to Gtk?




I am trying to add an image into a GUI using Gtk with Glade. Does anyone know how to do that or have a good website I can look at for help? I wasn’t able to find anything for Julia so I was wondering if anyone knows here. Thanks in advance!


Hi, you want to draw an image right?

So first you have to load the image into a 2D array. Then you can create a Canvas which is discussed here:

Then you can copy! the matrix into the Cairo context (i.e. copy!(ctx, my_image)). I am not exactly sure at the moment but when I do this the element type of the matrix is RGB{N0f8}. So the image is already colorized and available in RGB colors. When you load the image it might be the case that its already in this format.


I want to put in an image such as a picture from google onto the GUI


Here it is explained, the simplest way should be with GtkImage, @tobias.knopp 's method also works but is a bit more complicated if you start with say a PNG.


I tried following this example in the link you sent:

and did this:

the image i am trying to display is the egg.jpg but I keep getting the error "invalid escape sequence.

Do you have any suggestions on what I can do to fix this error? Thanks!


try escaping your backslashes "C:\\Users\\..."


Ah! That worked! Thanks so much!


I have another question.I’m trying to make the image fit inside the image widget now in the Glade GUI builder. Any suggestions/documentation on how to do that? Thanks!


Search for “GtkImage resize”. I’d guess all solutions (in C, C++, python etc.) put a resizing mechanism into the expose method (callback) of the respective widget.