Don't confuse bitmaps with .bmp files. Tk bitmaps and xpm files are handled differently from regular images like Bitmap( .bmp), jpgs, pngs, etc. Here is a basic bitmap (Tk terminology) usage:
#!/usr/bin/perl use Tk; my $mw = MainWindow->new; $mw->geometry('-5-0'); #$mw->overrideredirect(1); my @color = qw/red green/; my $bits = pack("b8"x8, "...11...", "..1111..", ".111111.", "11111111", "11111111", ".111111.", "..1111..", "...11...",); $mw->DefineBitmap('indicator' => 8,8, $bits); my $label = $mw->Label( -bitmap=>'indicator', -bg=>'black', -fg=>'red', )->pack; $mw->repeat(500,sub{$label->configure( -fg=>$color[0]); @color=reverse(@color); }); MainLoop;
To load any standard image type( bmp gif jpg png) you first load the image into a Photo object, then specify the Photo object in your widget's image option. Don't forget that gif and bmp are the only ones included by default, so you need use Tk::JPEG and use Tk::Png. Here is your basic image on a canvas.
#!/usr/bin/perl use warnings; use strict; use Tk; use Tk::JPEG; my $file = "zen16.jpg"; my $mw = Tk::MainWindow->new; my $can = $mw->Canvas( -width => 320, -height => 240 )->pack(); # if you are pulling your image from data (inline base64encoded string # you may need to specify -format #my $img = $mw->Photo( -data => $data, -format => 'jpeg' ); my $img = $mw->Photo( -file => $file); $can->createImage( 0, 0, -image => $img, -anchor => 'nw' ); my $red = $can->createRectangle(0, 20, 50, 75, -fill => 'red'); $can->Tk::bind("<Button-1>", [ \&print_xy, Ev('x'), Ev('y') ]); MainLoop(); sub print_xy { # print "@_\n"; my ($canv, $x, $y) = @_; print "(x,y) = ", $canv->canvasx($x), ", ", $canv->canvasy($y), "\n" +; printf "%6.6X\n", $img->get($canv->canvasx($x), $canv->canvasy($y) ) +; }

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

In reply to Re: Images in Canvas by zentara
in thread Images in Canvas by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.