2010-06-15 71 views
0

以下腳本將在Gnome桌面上進行截圖。將GTK python腳本轉換爲C

import gtk.gdk 

w = gtk.gdk.get_default_root_window() 
sz = w.get_size() 
pb = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB,False, 8, sz[0], sz[1]) 
pb = pb.get_from_drawable(w, w.get_colormap(), 0, 0, 0, 0, sz[0], sz[1]) 
if (pb != None): 
    pb.save("screenshot.png", "png") 
    print "Screenshot saved to screenshot.png." 
else: 
    print "Unable to get the screenshot." 

現在,我一直在試圖將其轉換爲C和在我寫但到目前爲止,我已經成功的應用之一使用它。 C(在Linux上)有什麼要做的嗎?

謝謝! Jess。

+1

到目前爲止你有什麼? – Ken 2010-06-15 14:04:13

+0

嗯,我首先嚐試添加一些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

+1

按照字面意思翻譯,'gtk.gdk.get_default_root_window'變成'gdk_get_default_root_window'等! – u0b34a0f6ae 2010-06-15 14:39:54

回答

3

我測試了這個,它確實有效,但是可能有一個更簡單的方法可以從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); 
+1

gdk_pixbuf_save:http://library.gnome.org/devel/gdk-pixbuf/unstable/gdk-pixbuf3-file-saving.html#gdk-pixbuf-save – u0b34a0f6ae 2010-06-15 14:58:00

+0

啊,沒有看到...應該使用在devhelp中搜索...好啊 – Spudd86 2010-06-15 15:22:08

+0

太棒了,謝謝。就像一個側面的問題,gdk_get_default_root_window()會給我的桌面,有沒有辦法獲得當前窗口? – Jessica 2010-06-15 15:29:43