使用 gtk 截图后不需要保存图片文件,直接通过内存中的数据进行显示。
首先安装 gtk :
sudo apt install gtk+-2.0 sudo apt install libgtk2.0*
然后编写应用程序:
#include <gtk/gtk.h> void destroy(void) { gtk_main_quit(); } 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; } int main (int argc, char** argv) { GtkWidget* window; GtkWidget* image; GdkPixbuf * pixbuf; gtk_init (&argc, &argv); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); // image = gtk_image_new_from_file(argv[1]); pixbuf = get_screenshot(); image = gtk_image_new_from_pixbuf(pixbuf); gtk_signal_connect(GTK_OBJECT (window), "destroy", GTK_SIGNAL_FUNC (destroy), NULL); gtk_container_add(GTK_CONTAINER (window), image); gtk_window_set_resizable(window, TRUE); gtk_widget_set_size_request(window, 800, 600); gtk_widget_show_all(window); gtk_main(); return 0; }
使用以下命令进行编译:
gcc -Wall displaytest.c -o displaytest `pkg-config --cflags gtk+-2.0` `pkg-config --libs gtk