Re: How can I save the pixel of a GdkWindow/GdkWidget to a file?



yinglcs aim com wrote:

Hi,

Can someone please tell me can I save the pixel of a GdkWindow/GdkWidget to a file?

The only think I find is saving from a GdkDrawable (the 2 functions below). But how can I get a GdkDrawable from a GdkWidget/GdkWindow?

There's no GdkWidget. GtkWidget has a pointer to corresponding drawable:
GTK_WIDGET(widget)->window
It can be NULL, however.

GdkPixbuf* pixBuf = gdk_pixbuf_get_from_drawable (NULL,
gd, NULL, 0, 0, 0, 0, w, h);

gdk_pixbuf_save (pixBuf, "snapshot.png", "png", &err, NULL);

I'd do the following:

gboolean
save_pixels (GtkWidget *widget, const gchar *fname)
{
   GdkPixbuf *pixbuf;

   g_return_val_if_fail (GTK_IS_WIDGET (widget) && fname != NULL, FALSE);

   if (widget->window != NULL) {
      gboolean res;

      /* it's typical for the widget to have no its own window,
        so you must specify the region it covers in the parent's window
        such an information is stored in GtkWidget::allocation
      */
      pixbuf = gdk_pixbuf_get_from_drawable (
         NULL,
         widget->window,
         NULL,
         widget->allocation.x, widget->allocation.y,
         0, 0,
         widget->allocation.width, widget->allocation.height,
      );

      g_return_val_if_fail (pixbuf != NULL, FALSE);

      res = gdk_pixbuf_save (pixbuf, fname, "png", NULL);

      g_object_unref (pixbuf);

      return res;
   }

   return FALSE;
}

PS: I've typed the code just in mail client, some typos are possible ;)

   Olexiy



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]