我正在使用 gtk+ 3.14,我想加载用户选择的图片。此处的代码正确显示图片但似乎没有更新 GdkPixbuf *pixbuf。事实上,GdkPixbuf 在函数内部不是 NULL,但在函数外部是 NULL。我该怎么做才能在其他功能中正确加载和使用 pixbuf?
这是回调结构:
struct callback_struct
{
GdkPixbuf *pix;
GtkWidget *img;
int height;
int width;
float scale;
};
这是我的代码:
void callback_load(gpointer data)
{
struct callback_struct *passed = data;
GdkPixbuf *pixbuf = passed->pix;
GtkWidget *image = passed->img;
int h_scr = passed ->height;
int w_scr = passed->width;
float scale = passed->scale;
GtkWidget *dial_box = gtk_file_chooser_dialog_new("Choose the image to load"
,GTK_WINDOW(gtk_widget_get_toplevel(image)),
GTK_FILE_CHOOSER_ACTION_OPEN,
"Cancel",GTK_RESPONSE_CANCEL,"Open",GTK_RESPONSE_ACCEPT,NULL);
switch (gtk_dialog_run (GTK_DIALOG (dial_box)))
{
case GTK_RESPONSE_ACCEPT:
{
gchar *filename =gtk_file_chooser_get_filename
(GTK_FILE_CHOOSER (dial_box));
pixbuf = gdk_pixbuf_new_from_file(filename,NULL);
printf("%d\n",(pixbuf == 0));
GdkPixbuf *scaled_buffer = gdk_pixbuf_scale_simple
(pixbuf,h_scr*scale,w_scr*scale,GDK_INTERP_BILINEAR);
gtk_image_set_from_pixbuf(GTK_IMAGE(image),scaled_buffer);
printf("%d\n",(pixbuf == 0));
gtk_widget_destroy(dial_box);
break;
}
case GTK_RESPONSE_CANCEL:
{
gtk_widget_destroy(dial_box);
break;
}
}
}