让图片随窗口大小变化进行缩放
首先在窗口中添加一个drawingarea,并使“GDK_EXPOSURE_MASK”触发有效。首加载图片到一个PIXBUF中,可以在程序初始做这件事。本例在窗口的“show”回调函数里做这事,代码如下:
pixbuf_src = gdk_pixbuf_new_from_file ("gtk-logo-rgb.gif", NULL); //将图片加载到pixbuf_src中
//下面两句是获取图片的宽和高。
original_width = gdk_pixbuf_get_width(pixbuf_src);
original_height = gdk_pixbuf_get_height(pixbuf_src);
接下来对图片进行缩放,在drawingarea的“expose”回调函数里做这件事。有两种方法。
第一种方法,用PIXBUF的缩放功能:
pixbuf_src = gdk_pixbuf_scale_simple (pixbuf_src,
(widget->allocation.width),
(widget->allocation.height),
GDK_INTERP_BILINEAR);
gdk_pixbuf_render_to_drawable (pixbuf_src,
widget->window,
widget->style->fg_gc,
0, 0,//src
0, 0,//dest
(widget->allocation.width),
(widget->allocation.height),
GDK_RGB_DITHER_NORMAL,
0, 0);
第二种方法,用cairo的缩放功能。
gdouble scale_x, scale_y;
cairo_t *cr;
scale_x = (widget->allocation.width) / (original_width*1.0);
scale_y = (widget->allocation.height) / (original_height*1.0);
cr = gdk_cairo_create (widget->window);
cairo_scale (cr, scale_x, scale_y);
gdk_cairo_set_source_pixbuf (cr, pixbuf_src, 0, 0);
cairo_paint (cr);
cairo_destroy (cr);
页:
[1]