john-jones
john-jones

Reputation: 7780

How to save entire scrollable canvas as PNG?

I have a scrollable canvas who's content i want into a png image.

The problem is I only get a photo of the canvas, missing the non visible part of the canvas at that given time.

How do i get the whole scrollable canvas into a png image?

My current code is the following:

my $canvas_to_get_photo=$mw->Photo(-format=>'Window', -data=>oct($canvas_to_get->id));
$canvas_to_get_photo->write('somepath/image.png', -format=>'png');

Upvotes: 0

Views: 1098

Answers (2)

Donal Fellows
Donal Fellows

Reputation: 137717

There isn't a native way to do it; Tk only paints to windows, not to image-based surfaces. Your options are therefore to either:

  1. scroll the canvas, taking snapshots, and then stitch them together
  2. generate encapsulated postscript (which does support going over the whole canvas, provided you use the right options) and generate your image from that with a tool like ghostscript.

Upvotes: 1

daotoad
daotoad

Reputation: 27193

It's been a awhile since I did any heavy Tk work, so this may not work. Have you tried looking for the non-scrolled subcomponent of the scrolled canvas.

IIRC, each 'Scrolled' widget is actually a "mega-widget", with scrollbars, a corner item, and a scrolled item sub-widgets.

So, it may be that you want

my $canvas = $scrolled->Subwidget('widget');
$canvas = $scrolled unless $canvas;
my $canvas_id = $canvas->id;

my $photo = $mw->Photo(-format => 'Window', -data => oct $canvas_id );
$photo->write('somepath/image.png', -format => 'png' );

Upvotes: 1

Related Questions