]> git.imager.perl.org - imager.git/blob - samples/tk-photo.pl
- extra concept index entries
[imager.git] / samples / tk-photo.pl
1 #!perl -w
2 use strict;
3 use Tk;
4 use Tk::Photo;
5 use MIME::Base64;
6 use Tk::PNG;
7 use Imager;
8
9 my $image = Imager->new(xsize=>100, ysize=>100);
10
11 # draw something simple here, you'll probably do something more complex
12 $image->box(filled=>1, color=>'blue');
13 $image->box(filled=>1, color=>'red', 
14             xmin=>20, ymin=>20, xmax=>79, ymax=>79);
15
16 my $image_data;
17 $image->write(data =>\$image_data, type=>'png')
18   or die "Cannot save image: ", $image->errstr;
19
20 # supplying binary data didn't work, so we base64 encode it
21 $image_data = encode_base64($image_data);
22
23 my $main = MainWindow->new;
24 my $tk_image = $main->Photo(-data => $image_data);
25 $main->Label(-image=>$tk_image)->pack;
26 MainLoop;
27
28 =head1 NAME
29
30 tk-photo.pl - display an Imager image under Tk
31
32 =head1 SYNOPSIS
33
34   $ perl tk-photo.pl
35
36 =head1 DESCRIPTION
37
38 Simple code to make a Tk::Photo object from an Imager image.
39
40 This works by:
41
42 =over
43
44 =item 1.
45
46 write the image data to a scalar in PNG format
47
48 =item 2.
49
50 Base64 decode the data
51
52 =item 3.
53
54 read() it into the photo object, supplying the Base64 encoded data to
55 the C<-data> parameter.
56
57 =back
58
59 =head1 REVISION
60
61 $Revision$
62
63 =head1 AUTHOR
64
65 Tony Cook <tony@imager.perl.org>
66
67 =cut