Re: Handling Gnome::Canvas data



On Fri, 23 Jun 2006 11:14:44 -0500
"Chris Bartak" <chrisbartak gmail com> wrote:

I've been trying to render a Gnome::Canvas to a pixbuf, without any luck.  I
read that in Perl you must use the pack() function to pack the pixel data
from the Gdk::Window into a scalar.  Unfortunately, I'm not sure how to get
the pixel data, or how to pack it properly.  Could anyone provide a quick
demo of how this is done?

Here is a little demo which captures the entire mainwindow. Just change
$window to $canvas in the sub, to get the canvas only.

One little glitch, the screen shot only gets created if you hit the Exit button,
Not if you hit the window manager destroy icon. I'm not sure why, but I'm
a bit rusty. :-)

#!/usr/bin/perl
use warnings;
use strict;
use Glib qw/TRUE FALSE/;
use Gtk2 '-init';
use Gnome2::Canvas;

my $window = Gtk2::Window->new('toplevel');
$window->set_title('Screenshot');
$window ->signal_connect( 'destroy' => \&delete_event );
$window->set_border_width(10);
#->set_size_request(300,200);
$window->set_default_size(250,400);

my $vbox = Gtk2::VBox->new( FALSE, 6 );
$vbox->set_size_request(0,0);
$window->add($vbox);
$vbox->set_border_width(2);

my $hbox= Gtk2::HBox->new( FALSE, 6 );
$vbox->pack_end($hbox,FALSE,FALSE,0);
$hbox->set_border_width(2);

$vbox->pack_end (Gtk2::HSeparator->new, FALSE, FALSE, 0);

my $button = Gtk2::Button->new_from_stock('gtk-quit');
$hbox->pack_end( $button, FALSE, FALSE, 0 );
$button->signal_connect( clicked => \&delete_event );

my $button1 = Gtk2::Button->new_from_stock('Screenshot');
$vbox->pack_start( $button1, FALSE, FALSE, 0 );
$button->signal_connect( clicked => \&screenshot );

my $canvas = Gnome2::Canvas->new();
$canvas->set_size_request(256,256);
$canvas->set_scroll_region(0,0,256,256);
my $root = $canvas->root();

my $item = Gnome2::Canvas::Item->new($root,
"Gnome2::Canvas::Ellipse",
x1 => 0,
y1 => 0,
x2 => 200,
y2 => 180,
fill_color=>"red",
outline_color=>"black"
);

$vbox->pack_start( $canvas, FALSE, FALSE, 0 );

$window->show_all();
Gtk2->main;
#####################################
sub delete_event {
Gtk2->main_quit;
return FALSE;
}  
#####################################
sub screenshot{

#we are going to save the entire mainwindow
my ($width, $height) = $window->window->get_size;

# create blank pixbuf to hold the image
my $gdkpixbuf = Gtk2::Gdk::Pixbuf->new ('rgb',
                    0,
                    8,
                    $width,
                    $height);

$gdkpixbuf->get_from_drawable ($window->window, 
             undef, 0, 0, 0, 0, $width, $height);

#only jpeg and png is supported !!!! it's 'jpeg', not 'jpg'
$gdkpixbuf->save ("$0.jpg", 'jpeg', quality => 100);
return FALSE;
}

#$pixbuf->save ($filename, 'jpeg', quality => '100');
#  Currently only a few parameters exist.  JPEG images can be saved
#  with a "quality" parameter; its value should be in the range
#  [0,100].  Text chunks can be attached to PNG images by specifying
#  parameters of the form "tEXt::key", where key is an ASCII string of
#  length 1-79.  The values are UTF-8 encoded strings.  


-- 
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html



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