6

I'm trying to take a screenshot of the entire screen with C and GTK. I don't want to make a call to an external application for speed reasons. I've found Python code for this (Take a screenshot via a python script. [Linux]); I just need to figure out how to do that in C.

Community
  • 1
  • 1
dieki
  • 2,435
  • 5
  • 23
  • 29
  • 1
    It rather looks like that Python code you linked to is a good crib into the GTK API calls you will need to use in C. – crazyscot Jun 26 '10 at 15:25
  • Hm, you're right. Between that and the gnome-screenshot code, I've figured it out. Thank you. – dieki Jun 26 '10 at 16:07
  • 1
    possible duplicate of [Convert a GTK python script to C](http://stackoverflow.com/questions/3045850/convert-a-gtk-python-script-to-c) – u0b34a0f6ae Jun 26 '10 at 17:29
  • @kaizer.se not really the same question, but my answer to that one works as answer to this :P – Spudd86 Jun 27 '10 at 02:19

2 Answers2

10

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!

dieki
  • 2,435
  • 5
  • 23
  • 29
  • 3
    Unfortunately this is now totally outdated and API's are removed since 3.10 – Lothar Apr 05 '17 at 23:35
  • 1
    @Lothar on Gtk# 3.24 **Windows** and **linux on xorg** you can use `new Pixbuf(Gdk.Global.DefaultRootWindow, rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height)` but it produces a transparent image on **wayland** (correct size but all transparent pixels) – Galacticai Apr 14 '23 at 03:22
  • Well it should only return transparent pixels until you have issued some permission requests first. But does anyone know how to do this? This has become pretty complicated with all the portals we have to use now. And unless Android Wayland programming is much less well documented. – Lothar Apr 14 '23 at 04:39
3

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

Alexander Dmitriev
  • 2,483
  • 1
  • 15
  • 20