DevOvercome
DevOvercome

Reputation: 151

How to load image from embedded resources

I want to set image to button:

GtkImage* imageForButton = gtk_image_new_from_resource("which path is here?");
GtkWidget* btn = gtk_button_new();
gtk_button_set_image(GTK_BUTTON(btn));

How to make it work with embedded resources?

I have resource.h/resource.rc, gonna try to use MakeIntResource, but I don't see any use of it for GTK.

Upvotes: 0

Views: 1407

Answers (1)

andlabs
andlabs

Reputation: 11588

GTK+ doesn't support Windows resources (at least as far as I can tell). Instead, it uses its own cross-platform resource format, GResource. The easiest way to get embedded resources working is to use this. It isn't compatible with Windows resource editors, and requires generating an extra C source file, but it should work fine so long as you use GLib or GTK+. You should have the GResource compiler as part of your GTK+ install (but I'm not 100% sure). And if you go this route, you'll be able to keep using PNG files. (I don't know how to use PNG files with Windows resources, if that even is possible.)

If you insist on using Windows resources, though, I can think of one possible way to do this, though this isn't the only one: use the Windows API resource functions to load an HBITMAP from your resource, call GetObject() to get the BITMAP structure from the HBITMAP, and then calling gdk_pixbuf_new_from_data() with the appropriate fields from the BITMAP structure as parameters (rowstride == BITMAP.bmWidthBytes). Good luck!

Upvotes: 2

Related Questions