Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Taking a screenshot with C\GTK

Tags:

c

screenshot

gtk

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.

like image 938
dieki Avatar asked Jun 26 '10 14:06

dieki


People also ask

How do you take a screenshot on a Wayland?

You don't need to write a tool as long as you use the reference compositor Weston for Wayland . It has this feature built in. You can use Super + S to take a screenshot and Super + R to record a video in *.

How do I take a screenshot in KDE Arch?

Shift + PrtScn - Take a screenshot of a section of the screen. Alt + PrtScn - Take a screenshot of the active window to users' home 'Pictures' directory. Ctrl + PrtScn - Take a screenshot of the whole screen to the clipboard.

How do you screenshot on a Windows C?

Press the Windows key + PrintScreen on your keyboard (or, PrtSc). Screenshots are automatically saved to Pictures/Screenshots in your user directory (C:/Users/%username%/Pictures/Screenshots by default).

What is the GTK_vector_screenshot protocol?

The protocol is simple and based on X window properties and X client messages: The gtk module sets the GTK_VECTOR_SCREENSHOT property for each top-level X window to some arbitrary value (currently "supported").

How to take a screenshot using C#?

This examples show how to take screenshot using C#. This method shows you a simple method of capturing screenshots using C# and .NET 2.0 ( CopyFromScreen () method. ) //Create a new bitmap. // Create a graphics object from the bitmap. // Take the screenshot from the upper left corner to the right bottom corner.

How to take a screenshot using GDI in C#?

This method shows you a simple method of capturing screenshots using C# and .NET 2.0 ( CopyFromScreen () method. ) //Create a new bitmap. // Create a graphics object from the bitmap. // Take the screenshot from the upper left corner to the right bottom corner. This method uses GDI in C#.NET to draw the primary screen onto a bitmap.

How to take a screenshot on a Windows PC?

On Windows 10 and 8, press Windows Key + PrtScn to capture the entire screen. On Windows 7 and earlier, press PrtScn. To capture only the active window, press Alt + PrtScn. To capture specific parts of the screen, use the Windows Snipping Tool or Snip & Sketch. This article explains how to take screenshots on a Windows PC.


2 Answers

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!

like image 94
dieki Avatar answered Nov 15 '22 11:11

dieki


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

like image 40
Alexander Dmitriev Avatar answered Nov 15 '22 12:11

Alexander Dmitriev