Taking a screenshot with C\GTK

After looking at the GNOME-Screenshot code and a Python example, I came up with this:

GdkPixbuf * get_screenshot(){
    GdkPixbuf *screenshot;
    GdkWindow *root_window;
    gint x_orig, y_orig;
    gint width, height;
    root_window = gdk_get_default_root_window ();
    gdk_drawable_get_size (root_window, &width, &height);      
    gdk_window_get_origin (root_window, &x_orig, &y_orig);

    screenshot = gdk_pixbuf_get_from_drawable (NULL, root_window, NULL,
                                           x_orig, y_orig, 0, 0, width, height);
    return screenshot;
}

Which seems to work perfectly. Thanks!


9 years passed and as mentioned above API is removed.

As far as I understand, currently the bare minimum to do this at Linux is:

GdkWindow * root;
GdkPixbuf * screenshot;
gint x, y, width, height;

root = gdk_get_default_root_window ();
gdk_window_get_geometry (root, &x, &y, &width, &height);
screenshot = gdk_pixbuf_get_from_window (root, x, y, width, height);
// gdk_pixbuf_save...

This is very slightly tested and may fail. Further reading is in gnome-screenshooter repo

Tags:

C

Gtk

Screenshot