GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

resize image when resizing window

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
pacofvf



Joined: 23 Apr 2008
Posts: 1

PostPosted: Wed Apr 23, 2008 5:02 pm    Post subject: resize image when resizing window Reply with quote

i get segmentation fault in this command:
g_object_set(image, "pixbuf", new_pixbuf, NULL);
why why why?
javascript:emoticon(':('), thanks
Code: (Plaintext)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91

#include <gtk/gtk.h>
#include <stdlib.h>
#include <stdio.h>
#include <glib.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <string.h>
int resized;
/* standard handlers */
gint delete_event(GtkWidget *widget, GdkEvent event, gpointer data)
{
   return FALSE;
}
void end_program(GtkWidget *widget, gpointer data)
{
   gtk_main_quit();
}
void render_image(GtkWidget *window, gpointer data)
{
  GtkImage *image;
  GtkButton *button;
  GdkPixbuf *orig_pixbuf, *new_pixbuf;
  gint orig_width, orig_height;
  gint new_width, new_height;
  gdouble zoom_value;
  if(resized!=0 && resized%2==0){
  image = (GtkImage *) data;
  /* get the original pixbuf dimensions */
  orig_pixbuf = (GdkPixbuf *)g_object_get_data(G_OBJECT(image), "orig&#8722;pixbuf");
  orig_width = gdk_pixbuf_get_width(orig_pixbuf);
  orig_height = gdk_pixbuf_get_height(orig_pixbuf);
  /* get adjuster&#8722;induced changes */
  zoom_value = 2;
  /* compute new size */
  new_width = (gint)(orig_width * zoom_value);
  new_height = (gint)(orig_height * zoom_value);
  /* prevent a height or width of 0 */
  new_width = MAX(1, new_width);
  new_height = MAX(1, new_height);
  /* scale the original pixbuf to the new dimensions
     (feel free to try other interpolation algorithms) */
  new_pixbuf = gdk_pixbuf_scale_simple(orig_pixbuf,
                                        new_width,
                                        new_height,
                                        GDK_INTERP_BILINEAR);
  /* display the new pixbuf in the image widget */
  g_object_set(image, "pixbuf", new_pixbuf, NULL);
  /* reference to new_pixbuf is no longer necessary,
     the system may dispose of it when convenient */
  g_object_unref(new_pixbuf);
  }
  resized++;
}
int main(int argc, char **argv)
{
  GtkWindow *window;
  GtkHBox *hbox;
  GtkImage *image;
  GtkVBox        *zoom_box,     *saturation_box;
  GtkButton       *button;
  GtkLabel        *label;
  GtkVScale      *zoom_slider,  *saturation_slider;
  GtkAdjustment  *zoom,         *saturation;
  GdkPixbuf *orig_pixbuf;
  resized=0;
  /* initialize GTK+, create a window */
  gtk_init(&argc, &argv);
  window = g_object_new(GTK_TYPE_WINDOW,"title", "resize 2","default&#8722;width", 300,"default&#8722;height", 300,"border&#8722;width", 12,NULL);
  /* attach standard event handlers */
  g_signal_connect(window, "delete_event", G_CALLBACK(delete_event), NULL);
  g_signal_connect(window, "destroy", G_CALLBACK(end_program), NULL);
  /* create image widget and load a file */
  image = g_object_new(GTK_TYPE_IMAGE, "file", "sí.jpg", NULL);
  /* store the original pixbuf in the image widget data */
  g_object_get(image, "pixbuf", &orig_pixbuf, NULL);
  g_object_set_data(G_OBJECT(image), "orig&#8722;pixbuf", (gpointer)orig_pixbuf);
  g_object_set(button, "image", image, NULL);
  /* install adjuster signal handlers */
  g_signal_connect(GTK_WIDGET(window), "size-request",
           G_CALLBACK(render_image), (gpointer) image);
  /* create a new HBox, pack the image and vboxes above */
  hbox = g_object_new(GTK_TYPE_HBOX, NULL);
  gtk_box_pack_start_defaults(GTK_BOX(hbox), GTK_WIDGET(image));
  /* pack everything into the window, show everything, start GTK+ main loop */
  gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(hbox));
  gtk_widget_show_all(GTK_WIDGET(window));
  gtk_main();
  return 0;
}

Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP