'GTK Image from String
I am trying to display an image based on a string of hex pixel data. The string object being transmitted to me is similar to the following:
std::string image_bytes = "0x00, 0x01, 0x02, etc...";
I am trying to process this data using the following code:
GtkWidget *image;
GdkPixbufLoader* loader = gdk_pixbuf_loader_new();
gdk_pixbuf_loader_write(loader, (const guchar*)image_bytes.data(), image_bytes.size(), nullptr);
gdk_image_set_from_pixbuf(GTK_IMAGE(image), loader);
This is giving me a headache of errors, any ideas?
Thanks
Solution 1:[1]
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;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Nate Morrison |