Press the button and the picture is saved in the current path as "Save.png":
#include <gtk/gtk.h> #include <cairo.h>/******************************************************** Function: Specify window area, need to include header file Cairo.h parameter: Area: Required window pointer x, y, W, H: Range Path_name: Save the path name of the picture, such as "./image/test.png" return value: Picture Pixbuf, Failed to return Null*********************************************************/gdkpixbuf *screenshot (GtkWidget *area, int x, int Y, int w, int h, char *path_name) {Gdkpixbuf *pixbuf = Null;pixbuf = Gdk_pixbuf_get_from_drawable (NULL, Area->window, NU LL, x, y, 0, 0, W, h); Take the picture of the rectangular area if (NULL = = pixbuf) {//If not obtained successfully, End Function printf ("gdk_pixbuf_get_from_drawable failed\n"); returnnull;} cairo_surface_t *surface = null;cairo_t *cr;//Create a Cairo appearance surface = Cairo_image_surface_create (Cairo_format_argb32, W , h);//a Cairo environment CR = Cairo_create (surface); The CR Drawing Gdk_cairo_set_source_pixbuf (CR, Pixbuf, 0, 0); Cairo_paint (CR);//Cairo_surface_write_to_png (surface, Path_ name);//Release the corresponding resource Cairo_destroy (CR); Cairo_surface_destroy (surface);//g_object_unref (PIXBUF); return pixbuf;} /* Features:Set Background * Widget: Main window * W, h: Picture size * Path: Picture path */void chang_background (gtkwidget *widget, int w, int h, const Gchar *PA Th) {gtk_widget_set_app_paintable (widget, TRUE);//Allow Windows to draw gtk_widget_realize (widgets);/* The picture overlaps when you change the background map * This is the function to call the following manually To invalidate the window drawing area, resulting in a window redraw event (that is, the expose event). */gtk_widget_queue_draw (widgets); Gdkpixbuf *src_pixbuf = gdk_pixbuf_new_from_file (path, NULL);//Create Picture Resource object//W, H is the width and height of the specified picture gdkpixbuf *dst_pixbuf = Gdk_ Pixbuf_scale_simple (Src_pixbuf, W, H, gdk_interp_bilinear); Gdkpixmap *pixmap = null;/* Create pixmap image; * NULL: no masks required; * 123:0~255, transparent to opaque */gdk_pixbuf_render_pixmap_and_mask (Dst_pixbuf, &pixmap, NULL, 128);//set a background map for the widget via Pixmap , the last parameter must be: Faslegdk_window_set_back_pixmap (Widget->window, Pixmap, FALSE);//Release Resources g_object_unref (SRC_PIXBUF); g_ Object_unref (DST_PIXBUF); G_object_unref (pixmap);} The processing function of the button "clicked" Signal is void Deal_button (Gtkwidget *w, gpointer data) {//, the specified window area is (0, 0, 300, 300), Save the picture in the current path as Save.pngscreenshot (Gtk_widget (data), 0, 0, +, "save.png");} int main (int ARGC, Char *argv[]) {gtk_init (&ARGC, &ARGV);//main window operation Gtkwidget *window = gtk_window_new (gtk_window_toplevel); Gtk_window_set_title (window), "Setting Windows Background") gtk_widget_set_size_request (window, gtk_window); chang_ Background (window, page, N, "image.jpg");//Set window background image gtkwidget *table = Gtk_table_new (3, 3, TRUE); Table layout, 3 rows 3 columns Gtk_container_add (gtk_container (window), table); Container Join window//button Gtkwidget *button = Gtk_button_new_with_label (""); gtk_table_attach_defaults (table), Button, 2 , 3, 2, 3); Handle the signal of the button, pass the window pointer to the callback function Deal_button () g_signal_connect (Button, "clicked", G_callback (Deal_button), window); gtk_ Widget_show_all (window); Show all parts gtk_main (); return 0;}
program run:
Cut out the picture:
Source code download please click here.
GTK Advanced Learning: GTK implementation Function (can specify range)