1.将以下代码保存到screenshot.c中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
#include <gtk/gtk.h> GdkPixbuf *get_screenshot() { GdkPixbuf *screenshot; GdkWindow *root; gint x, y; gint 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); return screenshot; } int main(int argc, char **argv) { GtkWidget *window; gtk_init(&argc, &argv); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_default_size(GTK_WINDOW(window), 0, 0); gtk_window_set_title(GTK_WINDOW(window), ""); GdkPixbuf *screenshot = get_screenshot(); GError *error=NULL; if (screenshot != NULL) { g_print("Not NULL p=%p\n", screenshot); gdk_pixbuf_save(screenshot, "test.jpeg", "jpeg",\ &error, "quality", "100", NULL); } else { g_print("NULL pointer\n"); exit(1); } g_signal_connect(G_OBJECT(window), "destroy", \ G_CALLBACK(gtk_main_quit),NULL); gtk_widget_show_all(window); if (access("test.jpeg", F_OK) == 0) { printf("截图成功\n"); exit(0); } gtk_main(); return 0; } |
2.终端执行以下代码打开文件时可以让检查器不报错
1 2 3 |
pkg-config --cflags gtk+-3.0 glib-2.0 | tr ' ' '\n' >.config_c |
3.编译
1 2 3 |
gcc `pkg-config --cflags --libs gtk+-3.0 ` screeshot.c |
4.运行
1 2 3 |
./a.out |