Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: GooCanvas to Cairo to Pixbuf

by renegadex (Beadle)
on Jul 25, 2008 at 10:54 UTC ( #700094=note: print w/replies, xml ) Need Help??


in reply to Re: GooCanvas to Cairo to Pixbuf
in thread GooCanvas to Cairo to Pixbuf

i am having trouble converting the cairo context into pixbuf.. how can this be done properly?

Replies are listed 'Best First'.
Re^3: GooCanvas to Cairo to Pixbuf
by zentara (Archbishop) on Jul 25, 2008 at 11:37 UTC
    #!/usr/bin/perl -w use strict; use warnings; use Goo::Canvas; use Gtk2 '-init'; use Glib qw(TRUE FALSE); my $window = Gtk2::Window->new('toplevel'); $window->signal_connect('delete_event' => sub { Gtk2->main_quit; }); $window->set_default_size(640, 600); my $vbox = Gtk2::VBox->new; $vbox->set_border_width(4); $vbox->show; $window->add($vbox); my $swin = Gtk2::ScrolledWindow->new; $swin->set_shadow_type('in'); $vbox->pack_start($swin, 1, 1, 0); my $canvas = Goo::Canvas->new(); $canvas->set_size_request(600, 450); $canvas->set_bounds(0, 0, 1000, 1000); $swin->add($canvas); my $root = $canvas->get_root_item(); my $rect = Goo::Canvas::Rect->new( $root, 100, 100, 400, 400, 'line-width' => 10, 'radius-x' => 20, 'radius-y' => 10, 'stroke-color' => 'yellow', 'fill-color' => 'red' ); my $text = Goo::Canvas::Text->new( $root, "Hello World", 300, 300, -1, 'center', 'font' => 'Sans 24', ); $text->rotate(45, 300, 300); # Create PNG + my $sb = Gtk2::Button->new_with_label('Write PNG and JPG'); + $vbox->pack_start($sb, FALSE, FALSE, 0); + $sb->show; + $sb->signal_connect("clicked", \&write_png_clicked, $canvas); + $window->show_all(); Gtk2->main; sub write_png_clicked { my ($but, $canvas) = @_; print "Write PNG...\n"; my $surface = Cairo::ImageSurface->create ('rgb24', 1000, 1000); # also argb32 is available # my $surface = Cairo::ImageSurface->create ('argb32', 1000, 1000); my $cr = Cairo::Context->create($surface); # make a background rectangle filled white so saved file looks same +as screen # otherwise a black background may appear, it's like pdf, if it isn' +t # drawn , it will be a black background, It won't automagically pick + up # a white background on a canvas $cr->rectangle( 0, 0, 1000, 1000 ); $cr->set_source_rgb( 1, 1, 1 ); $cr->fill; $canvas->render($cr, undef, 1); # this works, but see below for way to use pixbuf and jpg # my $status = $surface->write_to_png ("$0.png"); # print "$status\n"; my $loader = Gtk2::Gdk::PixbufLoader->new; $surface->write_to_png_stream (sub { my ($closure, $data) = @_; $loader->write($data); }); $loader->close; my $pixbuf = $loader->get_pixbuf; print $pixbuf->get_bits_per_sample(),"\n"; print $pixbuf->get_colorspace(),"\n"; $pixbuf->save ("$0.png", 'png'); print "done png\n"; $pixbuf->save ("$0.jpg", 'jpeg', quality => 100); print "done jpg\n"; return TRUE; }

    I'm not really a human, but I play one on earth Remember How Lucky You Are

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://700094]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2022-05-19 03:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (71 votes). Check out past polls.

    Notices?