Working with embedded images with GResource, GdkPixbuf and GtkWidget



Hello,



I need to show some images inside my GTK+ 2.32 application so I decided to embed them using GResource 
compiler.

I’ve prepared an XML file like this:



<?xml version="1.0" encoding="UTF-8"?>

<gresources>

  <gresource prefix="/myapp/icons">

    <file preprocess="to-pixdata">logo.png</file>
   <…..>

  </gresource>

</gresources>



And after “glib-compile-resources.exe --target=icons.c --generate icons.xml”, I correctly have a .c file with 
images encoded inside.



Problem here: i need that images to be shown in About dialog (gtk_show_about_dialog) and buttons 
(gtk_button_set_image). As far as I know, to accomplish this I have to create GdkPixbuf (for 
gtk_about_dialog_set_logo) and GtkWidget (for gtk_button_set_image). I tried with this code but got some 
issues (see the comments):



-----------

    GResource* icons = icons_get_resource();

    GBytes* data = g_resource_lookup_data(icons, path, G_RESOURCE_LOOKUP_FLAGS_NONE, NULL);

    gsite size = g_bytes_get_size(data); // <-- this works since I have no errors and the size is > 0, so I 
expect data to be ok



    // now I need to create a GdkPixbuf, is that the correct way?

    GError* e = NULL;

    GdkPixbuf* pixbuf;

    pixbuf = gdk_pixbuf_new_from_inline(g_bytes_get_size(data), data, FALSE, &e);
    // error here! Message is “Image header corrupt”



    // if worked… I’d continue by also getting the GtkWidget image with:
    GtkWidget* image;

    image = gtk_image_new_from_pixbuf(pixbuf);

-----------



I can’t really understand how Pixbuf expects from the GResource data… also i read in the docs that 
“gdk_pixbuf_new_from_inline has been deprecated since version 2.32 and should not be used in newly-written 
code. Use GResource instead.”… so what’s the correct way to create Pixbuf for About boxes and GdkWidgets for 
image button?



Thanks for the support

Alessandro





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]