Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Images in Canvas

by Anonymous Monk
on Nov 26, 2008 at 09:19 UTC ( [id://726044]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks,
I am trying to add a jpg file to my canvas but of no go. Here is the simple code that i am working with:
use Tk; use strict; my $mw = new MainWindow; $mw->geometry('600x600+110+110'); my $drawing = $mw->Canvas(-width => 500, -height => 500, -background => "white") -> pack; my $xpm = $drawing->createBitmap(0, 0, -bitmap => '@myfile.bmp'); my $xpm = $drawing->createImage(0, 0, -bitmap => '@myfile.jpg'); #$drawing->createOval(100,50,300,250,-fill=>"black"); #$drawing->createRectangle(150,100,250,200,-fill=>"red"); #$drawing->createText(200,275,-text=>"Some text on my drawing!",-ancho +r=>"center"); MainLoop();
Except the Bitmap and the image, i am able to create all other shapes (the lines that are commented) in the Canvas. I am receiving the below error message format error in bitmap dataerror reading bitmap file "myfile.bmp" at C:/Perl/site/lib/Tk.pm line 250.
Am i doing any thing wrong. Please guide me in right direction.

Replies are listed 'Best First'.
Re: Images in Canvas
by Crian (Curate) on Nov 26, 2008 at 09:41 UTC

    You use my $xpm two times, I think one of that lines should be commented out.

    In different to the createBitmap method you do not need to put a @ in front of the image name when using createImage, if I understand "Mastering Perl/TK" the right way.

    Perhaps try one of the built in bitmaps first, to be shure to have no problems with the bitmap file, e.g. my $xpm = $drawing->createBitmap(0, 0, -bitmap  => 'info');.

    In case of the createImage method, you have to use an object created with the Photo or Bitmap methods.


    I tested a little bit and ending with this, perhaps it is a source for an idea for you:

    #!/usr/bin/perl use strict; use warnings; use Tk; my $mw = new MainWindow; $mw->geometry('600x600+110+110'); my $drawing = $mw->Canvas( -width => 500, -height => 500, -background => 'white', )->pack(); my $xpm = <<'END'; /* XPM */ static char *test[]={ "32 32 11 1", ". c None", "# c #000000", "h c #004000", "g c #004040", "f c #404000", "e c #808080", "i c #a0a0a4", "c c #c0c000", "d c #ffff00", "b c #ffffc0", "a c #ffffff", "................................", "................................", "................................", "................................", ".............#....###...........", "............#a##.#aba##.........", "...........#aaaa#aaaaaa##.......", "..........#aaaa#aaaaaaaba##.....", ".........#aaaa#aaaaaaaaaaab##...", "........#aaba#aaaaaaabaabaaaa##.", ".......#aaba#baaaaa#baaaaaabaaa#", "......#aaaa#aaaaab#a##baaaaaaa#.", ".....#aaba#aacbba#aaaa##baaab#..", "....#bbaa#abaacc#aaaaaaa##aa#...", "...#bbbb#aaabaa#aaabaaaaaa##....", "..#bbbb#bacaaa#aaaaaaaabaaaa##..", ".#bbbb#bbaacc#aacaaaaaaaaaaaba#.", "#ddbb#bbbbba#aaaaccaaaaaaaaaaaa#", "#edddfgcbbbfaacaaaaccaaaaaaaaa#e", ".#hedddfhcfabaaccaaabccaaaaaa#e.", "...##edddhaacbbaaccbaaaccaab#i..", ".....##e#baaaccabaaccbaabaa#i...", "......##bacbaabccbaaaccabb#e....", "......#bbbbccaabbccaaaaab#i.....", ".....#bbbbbbbccabbaccaab#i......", ".....#ebbbbbbbbccbaabba#i.......", "......##ebbbbbbbbccaabhe........", "........##ebbbbbbbbbb#e.........", "..........##ibbbbbbbhe..........", "............##ebbbb#e...........", "..............#hee#i............", "................##i............."}; END my $bitmap = $mw->Pixmap(-data => $xpm); $drawing->createImage(100, 100, -image => $bitmap); MainLoop();
Re: Images in Canvas
by zentara (Archbishop) on Nov 26, 2008 at 14:52 UTC
    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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-04-25 16:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found