转换GTK python脚本到C [英] Convert a GTK python script to C

查看:218
本文介绍了转换GTK python脚本到C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的脚本将采取Gnome桌面上的截图。

The following script will take a screenshot on a Gnome desktop.

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上)?

Now, I've been trying to convert this to C and use it in one of the apps I am writing but so far i've been unsuccessful. Is there any what to do this in C (on Linux)?

谢谢!
杰斯。

Thanks! Jess.

推荐答案

我测试了这一点,它的工作,但有可能是一个简单的从GdkPixbuf去的方式为PNG这只是我第一个发现。 (有没有 gdk_pixbuf_save()

I tested this and it does work, but there might be a simpler way to go from GdkPixbuf to a png this was just the first one I found. (There's no 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)

you'd compile like this: (assuming you save it as screenshot.c)

gcc -std=gnu99 `pkg-config --libs --cflags gdk-2.0` screenshot.c -o screenshot

编辑:东西保存的pixbuf也可以是这样的:(注意我没有尝试这一点,但它只是一条线......)由于kaizer.se指出我在阅读文档失败:P

the stuff to save the pixbuf could also look like: (note I didn't try this out, but it's only one line...) Thanks to kaizer.se for pointing out my fail at doc reading :P

gdk_pixbuf_save(pb, "screenshot.png", "png", NULL, NULL);

这篇关于转换GTK python脚本到C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆