Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 869539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:19:24+00:00 2026-05-15T10:19:24+00:00

The following script will take a screenshot on a Gnome desktop. import gtk.gdk w

  • 0

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."

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T10:19:25+00:00Added an answer on May 15, 2026 at 10:19 am

    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;
    }
    

    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
    

    Edit: 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 😛

    gdk_pixbuf_save(pb, "screenshot.png", "png", NULL, NULL);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following script will update the database, but it won't display the correct page
I thought the following script will create div element but I got nothing output
I have the following script which will delete a file off a BlackBerry. However,
The following script is returning false, when I think that it should be returning
I have a python script that will take each file in a directory, iterate
I am working to create a script which will take a string as an
I have the following script which will change the background image of a text
I've been looking all over for a script that will take care of the
I'm trying to write a piece of code that will do the following: Take
Take a look at the following link, you will see I have a simple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.