This is in C, but it should work in C++.
In your code, the hex data is stored as an array of strings (char *str[]
), not a string (char *str
); by doing so, 0x21
will be "0x21"
, not !
.
It also appears that you did not initialise image
. This will cause a segmentation fault.
loader
as the second argument of gtk_image_set_from_pixbuf()
is an incompatible pointer to GdkPixbufLoader *
from GdkPixbuf *
.
/* GtkPixbufLoader demo program */
#include <gtk/gtk.h>
#include <string.h>
int main(int argc, char **argv) {
GtkWidget *window, *image;
GdkPixbufLoader *loader;
// This is just random data from the beginning of a JPEG file.
unsigned char data[] = {0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52};
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
image = gtk_image_new();
loader = gdk_pixbuf_loader_new();
/* Create pixbuf from data and set the image from `loader` */
gdk_pixbuf_loader_write(loader, data, strlen((char *) data), NULL);
gtk_image_set_from_pixbuf(GTK_IMAGE(image), gdk_pixbuf_loader_get_pixbuf(loader));
gtk_container_add(GTK_CONTAINER(window), image);
gtk_widget_show_all(GTK_WIDGET(window));
gtk_main();
return 0;
}