Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^3: Bitmap for beginners (Perl Tk)

by mawe (Hermit)
on Feb 16, 2005 at 08:37 UTC ( [id://431467]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Bitmap for beginners (Perl Tk)
in thread Bitmap for beginners (Perl Tk)

Hi!

As zentara mentions below, you have to base64 encode the image first. Here is a snippet from Matering Perl/Tk which shows one was to do it:

sub encode_photo_data { my($file) = @_; use MIME::Base64; my ($bin, $data, $stat); open PHOTO, $file or die "Cannot open $file: $!"; while ( $stat = sysread PHOTO, $bin, 57 * 17 ) { $data .= encode_base64($bin); } close PHOTO or die $!; die "sysread error: $!" unless defined $stat; $data; } # end encode_photo_data

Regards, mawe

Replies are listed 'Best First'.
Re^4: Bitmap for beginners (Perl Tk)
by TonyDonker (Novice) on Feb 16, 2005 at 09:00 UTC
    Hi mawe,

    OK - so I tried this:

    use Tk;
    my $top = new MainWindow;
    my $pic = $top->Photo(-file=>encode_photo_data("up.bmp"));
    $top->Button(-image=>$pic)->pack();
    MainLoop();

    sub encode_photo_data {
    my($file) = @_;
    use MIME::Base64;
    my ($bin, $data, $stat);
    open PHOTO, $file or die "Cannot open $file: $!";
    while ( $stat = sysread PHOTO, $bin, 57 * 17 ) {
    $data .= encode_base64($bin);
    }
    close PHOTO or die $!;
    die "sysread error: $!" unless defined $stat;
    $data;
    } # end encode_photo_data

    now it returns 'Cannot open + a lot of binary data...?

    What am I doing wrong?
Re^4: Bitmap for beginners (Perl Tk)
by TonyDonker (Novice) on Feb 16, 2005 at 09:47 UTC
    I receive the message:
    Cannot open '

    ***lots of binary data***

    ' in mode 'r' at ../Image.pm line 21
      Hi!

      The data you receive from encode_photo_data() is not a file, so you have to use Photo(-data=>...):

      $encoded_pic = encode_photo_data("up.bmp"); my $pic = $top->Photo(-data=>$encoded_pic);
      or in one line:
      my $pic = $top->Photo(-data=>encode_photo_data("up.bmp"));
      Regards, mawe
        This works also fine:

        use Tk;<br/> my $top = new MainWindow;<br/> my $pic = $top->Photo(-file=>"img/hals.gif");<br/> $top->Button(-image=>$pic)->pack();<br/> MainLoop();<br/><br/>
        a .GIF instead of a .BMP

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-04-23 21:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found