我測試了這個,它確實有效,但是可能有一個更簡單的方法可以從GdkPixbuf到png,這只是我找到的第一個。 (有沒有gdk_pixbuf_save()
)
#include <unistd.h>
#include <stdio.h>
#include <gdk/gdk.h>
#include <cairo.h>
int main(int argc, char **argv)
{
gdk_init(&argc, &argv);
GdkWindow *w = gdk_get_default_root_window();
gint width, height;
gdk_drawable_get_size(GDK_DRAWABLE(w), &width, &height);
GdkPixbuf *pb = gdk_pixbuf_get_from_drawable(NULL,
GDK_DRAWABLE(w),
NULL,
0,0,0,0,width,height);
if(pb != NULL) {
cairo_surface_t *surf = cairo_image_surface_create(CAIRO_FORMAT_RGB24,
width, height);
cairo_t *cr = cairo_create(surf);
gdk_cairo_set_source_pixbuf(cr, pb, 0, 0);
cairo_paint(cr);
cairo_surface_write_to_png(surf, "screenshot.png");
g_print("Screenshot saved to screenshot.png.\n");
} else {
g_print("Unable to get the screenshot.\n");
}
return 0;
}
你編譯如下:(假設你將它保存爲screenshot.c)
gcc -std=gnu99 `pkg-config --libs --cflags gdk-2.0` screenshot.c -o screenshot
編輯:東西保存pixbuf的也可以是這樣的:(注意我沒有試過這個,但它只是一行......)感謝kaizer.se指出我在doc閱讀中的失敗:P
gdk_pixbuf_save(pb, "screenshot.png", "png", NULL, NULL);
到目前爲止你有什麼? – Ken 2010-06-15 14:04:13
嗯,我首先嚐試添加一些GTK(參見http://maemo.org/api_refs/5.0/beta/hildon/hildon-Additions-to-GTK+.html以及http:// maemo中的截圖示例.gitorious.org/hildon/hildon/blobs/hildon-2-2/examples/hildon-gtk-window-take-screenshot-sync.c)但帶來了地獄的依賴,然後我嘗試了XGetImage(http:// tronche.com/gui/x/xlib/graphics/XGetImage.html),但該代碼需要Xorg開發庫來編譯...你可以在這裏看到一個樣本:http://www.codase.com/ search/call?name = xgetimage,現在我被卡住了 – Jessica 2010-06-15 14:27:33
按照字面意思翻譯,'gtk.gdk.get_default_root_window'變成'gdk_get_default_root_window'等! – u0b34a0f6ae 2010-06-15 14:39:54