简体   繁体   English

用C \\ GTK截取屏幕截图

[英]Taking a screenshot with C\GTK

I'm trying to take a screenshot of the entire screen with C and GTK. 我正试图用C和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] ); 我已经为此找到了Python代码( 通过python脚本截取屏幕截图。[Linux] ); I just need to figure out how to do that in C. 我只需要弄清楚如何在C中做到这一点。

After looking at the GNOME-Screenshot code and a Python example, I came up with this: 在查看GNOME-Screenshot代码和Python示例后,我想出了这个:

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. 9年过去了,如上所述API被删除。

As far as I understand, currently the bare minimum to do this at Linux is: 据我所知,目前在Linux上做到这一点的最低要求是:

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 进一步阅读是在gnome-screenshooter repo中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM