Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

GooCanvas to Cairo to Pixbuf

by renegadex (Beadle)
on Jul 25, 2008 at 08:26 UTC ( #700052=perlquestion: print w/replies, xml ) Need Help??

renegadex has asked for the wisdom of the Perl Monks concerning the following question:

does anybody have an example on how to convert goocanvas drawings to a pixbuf? i saw an example on how to save to pdf. but it did not help.. i wanted to convert the rendered cairo context into an imagesurface then using pixbufloader i would convert it into pixbuf. then using pixbuf i can save it to jpeg. :D this is what i did..
sub write_pdf_clicked { print "Write PDF...\n"; my $surface = Cairo::ImageSurface->create("a8", 2100, 1500); my $cr = Cairo::Context->create($surface); $cr->translate(0, 0); $canvas->render($cr, undef, 1); $cr->show_page; #print $surface; my $pixbufloader = Gtk2::Gdk::PixbufLoader->new; $pixbufloader->write($surface); $pixbufloader->close; my $pixbuf = $pixbufloader->get_pixbuf; return 1; }

Replies are listed 'Best First'.
Re: GooCanvas to Cairo to Pixbuf
by zentara (Archbishop) on Jul 25, 2008 at 10:34 UTC
    Cairo dosn't do jpg's, probably because it dosn't have transparencies. But the pixbuf can convert.
    $pixbuf->save ("$0.jpg", 'jpeg', quality => 100);

    I'm not really a human, but I play one on earth Remember How Lucky You Are
      i am having trouble converting the cairo context into pixbuf.. how can this be done properly?
        #!/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?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://700052]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2022-05-17 18:33 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (68 votes). Check out past polls.