http://www.perlmonks.org?node_id=112816

This is the second demonstration program I've written for wxperl. Jouke mentioned that my last tutorial was better suited to someone familiar with wxperl than not, and I've tried to keep the same level of detail & explanation here.
This script shows how to load and display a file (in this case, a jpeg) into a bitmap control. I found that wxWindows' documentation was unclear on how image handlers interacted with image controls and bitmap controls, and it took a little under two hours to come up with the 9 (!) lines of code to complete the task. It also shows how to implement constraints to keep resized objects looking good.
You'll have to change the name of the file being loaded, but otherwise the script should load as-is.
#!/usr/bin/perl use Wx; package MyApp; #As always, we need to subclass Wx::App and populate it with a frame. use strict; use vars qw(@ISA); @ISA=qw(Wx::App); sub OnInit { my $this = @_; my $frame = MyFrame->new( "Mini-image demo", [-1,-1], [-1,-1]); # if the frame didn't get created, exit early. don't use die here, as +it seems to be a little unstable... # if OnInit returns false,though, wx will clean up after itself nicely +. unless ($frame) {print "unable to create frame -- exiting."; return +undef} $frame->Show( 1 ); 1; #everything's ok, on with the show. } package MyFrame; # subclass wx::Frame to insert the image control. use vars qw(@ISA); use strict; # # All we load here are 2 constants used # to keep the image stretched to the dimensions of the window. # use Wx qw( wxWidth wxHeight); # # Wx::Image loads the Image control and all of the Image handlers. # use Wx::Image; use IO::File; @ISA=qw(Wx::Frame); sub new { my $class = shift; my $this = $class->SUPER::new( undef, -1, $_[0], $_[1], $_[2] ); # # replace the filename with something appropriate. # my $file = IO::File->new( "c:/halo2.jpg", "r" ) or return undef; binmode $file; # # define a handler for jpeg images. # handlers are half of what translates file formats (.jpg in thi +s case) # into streams readable by wxBitmap objects for diplay. # yes, this means you typically can't cram a file's contents int +o # a wxBitmap control -- the exception being windows bitmaps. # # There is a method called 'Wx::Image::FindHandlerType', but I c +ouldn't # get the thing to work in any way, shape or form. # my $handler = Wx::JPEGHandler->new(); # # wxImages are the other half of getting images displayed -- # they hold the data stream created by a handler. # Although there's many functions that can be performed with an +wxImage, # all we're interested is turning the stream into a bitmap. # my $image = Wx::Image->new(); my $bmp; # used to hold the bitmap. # # here we load the file into $image. simple enough. # $handler->LoadFile( $image, $file ); # # and here, we turn the data in $image into a bitmap, and store +in $bmp. # if the data in $bmp is bad for some reason, $bmp->Ok() will fa +il. $bmp = $image->ConvertToBitmap(); if( $bmp->Ok() ) { # create a static bitmap called ImageViewer that displays the # selected image. $this->{ImageViewer}= Wx::StaticBitmap->new($this, -1, $bmp); } # At this point, we could just return the frame, and be ok. # unfortunately, the static bitmap could be *really* static and +not # resize itself when the window's resized, which'd look pretty u +gly. # # To make sure everything looks ok, we'll assign constraints to # Imageviewer. # Layout constraints determine how controls will appear within t +he # window. They consist of 4 members : left, top, width and heigh +t. # constraints are objects by themselves, and need to be assigned + to # objects in order to have any use. We'll be using this contstra +int # in order to stretch the picture out to the full size of the wi +ndow. # my $b1 = Wx::LayoutConstraints->new(); # set the upper left corner to absolute [0,0]. # if there were other controls using constraints, we could change +these to # something like $b1->left->RightOf ($widget, 5) to make this cont +rol appear # 5 pixels to the right of $widget's constraints. $b1->left->Absolute(0); $b1->top->Absolute(0); # now set the width and height to 100% each way. $b1->width->PercentOf( $this, wxWidth,100); $b1->height->PercentOf( $this, wxHeight, 100); # # Now that all the dimensions of the constraint are complete, # assign it to ImageViewer. # $this->{ImageViewer}->SetConstraints( $b1 ); $this; # return the frame object to the calling application. } package main; my $app = MyApp->new();# create an instance of the Wx::App-derived cla +ss $app->MainLoop(); # run

Replies are listed 'Best First'.
Re: wxPerl image handling (short & sweet). (wxPerl Tutorial 2)
by Anonymous Monk on Aug 28, 2003 at 15:00 UTC
    Any chance of renaming this to have wxPerl tutorial 2 in it to aid finding it in simple search? ;)
Re: wxPerl image handling (short & sweet).
by Anonymous Monk on Sep 03, 2003 at 20:27 UTC
    I installed the Wx-016 package on Windows 95 - the Wx::Image module isn't there. Have I done something silly? (The code from tutorial 1 runs fine.)
        Thanks - that fixed it.

        I also had to change the ConvertToBitmap method call (listed as deprecated in the doco, and not found at runtime) to a Wx::Bitmap->new call.

        So now it runs, but photos look just awful. It looks like there isn't enough colour depth, but $bmp->getDepth() returns 24.

      As of today the following works. I made three changes:

      • Removed use Wx::Image;
      • Replaced the image file name with one on my system (ie Create "c:\\halo2.jpg" or change the file name and/or path.)
      • Replaced $bmp = $image->ConvertToBitmap(); with $bmp=Wx::Bitmap->new( $image );
      Here is the full code (don't forget to change the image path and name):

      #!/usr/bin/perl use Wx; package MyApp; #As always, we need to subclass Wx::App and populate it with a frame. use strict; use vars qw(@ISA); @ISA=qw(Wx::App); sub OnInit { my $this = @_; my $frame = MyFrame->new( "Mini-image demo", [-1,-1], [-1,-1]); # if the frame didn't get created, exit early. don't use die here, as +it seems to be a little unstable... # if OnInit returns false,though, wx will clean up after itself nicely +. unless ($frame) {print "unable to create frame -- exiting."; return +undef} $frame->Show( 1 ); 1; #everything's ok, on with the show. } package MyFrame; # subclass wx::Frame to insert the image control. use vars qw(@ISA); use strict; # # All we load here are 2 constants used # to keep the image stretched to the dimensions of the window. # use Wx qw( wxWidth wxHeight); # # Wx::Image loads the Image control and all of the Image handlers. # use IO::File; @ISA=qw(Wx::Frame); sub new { my $class = shift; my $this = $class->SUPER::new( undef, -1, $_[0], $_[1], $_[2] ); # # replace the filename with something appropriate. # my $file = IO::File->new( "c:\\halo2.jpg", "r" ) or return undef; binmode $file; # # define a handler for jpeg images. # handlers are half of what translates file formats (.jpg in thi +s case) # into streams readable by wxBitmap objects for diplay. # yes, this means you typically can't cram a file's contents int +o # a wxBitmap control -- the exception being windows bitmaps. # # There is a method called 'Wx::Image::FindHandlerType', but I c +ouldn't # get the thing to work in any way, shape or form. # my $handler = Wx::JPEGHandler->new(); # # wxImages are the other half of getting images displayed -- # they hold the data stream created by a handler. # Although there's many functions that can be performed with an +wxImage, # all we're interested is turning the stream into a bitmap. # my $image = Wx::Image->new(); my $bmp; # used to hold the bitmap. # # here we load the file into $image. simple enough. # $handler->LoadFile( $image, $file ); # # and here, we turn the data in $image into a bitmap, and store +in $bmp. # if the data in $bmp is bad for some reason, $bmp->Ok() will fa +il. #$bmp = $image->ConvertToBitmap(); $bmp=Wx::Bitmap->new( $image ); if( $bmp->Ok() ) { # create a static bitmap called ImageViewer that displays the # selected image. $this->{ImageViewer}= Wx::StaticBitmap->new($this, -1, $bmp); } # At this point, we could just return the frame, and be ok. # unfortunately, the static bitmap could be *really* static and +not # resize itself when the window's resized, which'd look pretty u +gly. # # To make sure everything looks ok, we'll assign constraints to # Imageviewer. # Layout constraints determine how controls will appear within t +he # window. They consist of 4 members : left, top, width and heigh +t. # constraints are objects by themselves, and need to be assigned + to # objects in order to have any use. We'll be using this contstra +int # in order to stretch the picture out to the full size of the wi +ndow. # my $b1 = Wx::LayoutConstraints->new(); # set the upper left corner to absolute [0,0]. # if there were other controls using constraints, we could change +these to # something like $b1->left->RightOf ($widget, 5) to make this cont +rol appear # 5 pixels to the right of $widget's constraints. $b1->left->Absolute(0); $b1->top->Absolute(0); # now set the width and height to 100% each way. $b1->width->PercentOf( $this, wxWidth,100); $b1->height->PercentOf( $this, wxHeight, 100); # # Now that all the dimensions of the constraint are complete, # assign it to ImageViewer. # $this->{ImageViewer}->SetConstraints( $b1 ); $this; # return the frame object to the calling application. } package main; my $app = MyApp->new();# create an instance of the Wx::App-derived cla +ss $app->MainLoop(); # run
        Big thank you for this tutorial. It has been a massive help. ^_^
        This script didn't run for me. The error message is: OnInit must return a true return value at C:\RPD Programming\RPD wxPerl\Perlmonk s wxPerl tutorial\image.pl line 119 unable to create frame -- exiting. C:\RPD Programming\RPD wxPerl\Perlmonks wxPerl tutorial> What happened here?