Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: wxPerl: is BitmapFromImage implemented?

by jmlynesjr (Hermit)
on Apr 28, 2013 at 22:20 UTC ( #1031116=note: print w/replies, xml ) Need Help??

in reply to wxPerl: is BitmapFromImage implemented?


I didn't find BitmapFromImage either, but by interpreting the hieroglyphics of the wxBook, the following works for me. You'll have to reset the image filename to your test jpg file. Note also that the StaticBitmap documentation lists a 64x64 size limit under Windows 9x that I don't see here on Ubuntu, but you might. YMMV.

The hint was:

// Load an Image wxImage image(wxT("image.png"), wxBITMAP_TYPE_PNG); // Convert it to a bitmap wxBitmap bitmap(image);
#! /home/xxxx/CitrusPerl/perl/bin/perl # Note I prefer to point to my CitrusPerl install rather than running +a shell script everytime I want to run perl. # Image test - image manipulation use strict; use warnings; use 5.014; package MyForm; # ----------------------------------------- packag +e MyForm:: use strict; use warnings; use Wx qw[:everything]; use Wx::Event qw(EVT_BUTTON ); use parent -norequire, 'Wx::Frame'; use Wx qw(:id :toolbar :statictext wxNullBitmap wxDefaultPosition wxDe +faultSize wxNullBitmap wxTB_VERTICAL wxSIZE wxBITMAP_TYPE_ANY wxBITMAP_TY +PE_BMP); sub new { #1 --------------------------- new MyForm:: my $class = shift; my %parms = @_; my $self = $class->SUPER::new( undef, -1, 'Image test', wxDefaultPosition, # position wxDefaultSize, # size ); my ( $img, $sb, $sizer); my $panel = Wx::Panel->new( $self, -1, # parent window, ID wxDefaultPosition, wxDefaultSize); $panel->SetBackgroundColour(Wx::Colour->new('light blue')); my $welcome = Wx::StaticText->new($panel, -1, 'Welcome to Image test', + [150, 250], [-1, -1], wxALIGN_CENTER); $sizer = Wx::BoxSizer->new(wxVERTICAL); Wx::InitAllImageHandlers; # Added $img = Wx::Image->new($parms{image_fname}, wxBITMAP_TYPE_ANY); my $bmp = Wx::Bitmap->new($img); # Added $sb = Wx::StaticBitmap->new($panel, -1, $bmp); # change +d $sizer->Add( $welcome, 0, wxALL | wxEXPAND, 5 ); $sizer->Add( $sb, 0, wxALL | wxEXPAND, 5 ); $panel->SetSizer($sizer); $panel->Fit; return $self; } #1 end sub new MyForm:: 1; # end package MyForm package main; # ----------------------------- package main +:: use strict; use warnings; use 5.014; my $image_file = '/home/xxxx/CitrusPerl/perl/citrus/lib/Wx/DemoModules +/files/splash/logo.jpg'; # Changed my $app = Wx::SimpleApp->new; my $frame = MyForm->new( image_fname => $image_file); $frame->Show(1); $app->MainLoop; 1;


There's never enough time to do it right, but always enough time to do it over...

Replies are listed 'Best First'.
Re^2: wxPerl: is BitmapFromImage implemented?
by HelenCr (Monk) on Apr 28, 2013 at 23:56 UTC
    Thank you, James. This is great - it's working fine. You did a great job of deciphering hieroglyphics.

    Now the image shows great, but when you resize the window, it "crops" the image - it displays only the image part which is covered by the window (in other words, it does not compress the image size so that regardless of the actual window size, the entire image will always be shown).

    Can you suggest how to do it? (I could set up a resize event, and then in the event handler subroutine, change the image size and re-display, but this sounds a bit like a kludge, it there an automatic way to do it?)

    Many TIA - Helen

    (Running DWIM Perl 5.14.2 on Windows 7 and WinXP SP 3)

      Helen, good question...

      No idea off hand. Seems I read about sizers that the control "communicates" a minimum size to the sizer process. Maybe there is a clue there. Most of the documentation refers to stretching controls not compressing them. Your "kludge" may be the way to go, at least until another idea surfaces.

      Once again from the wxBook(well worth $38 to Amazon):

      "(Sizers)...It is based upon the idea of individual windows reporting their minimal required size and their ability to be stretched if the size of the parent window has changed. This will most often mean that the programmer does not set the initial size of the dialog; instead, the dialog will be assigned a sizer, which will be queried about the recommended size....The minimal size of a control is calculated from the control's notion of it's "best size"(supplied by implementing DoGetBestSize for each control)....However, if you want the initial window size(as passed to the window's constructor) to be used as the initial minimum size instead, you can use the wxFIXED_MINSIZE style when adding the control to the sizer. Note that only some windows can calculate their size....whereas others....don't have any natural width or height and thus require an explicit size."

      Update1: The Wx::Image::Rescale seems to work. Try a subset of this in your resize event handler. Two scaling qualities are supported: wxIMAGE_QUALITY_NORMAL and wxIMAGE_QUALITY_HIGH(better quality but slower).

      Wx::InitAllImageHandlers; $img = Wx::Image->new($parms{image_fname}, wxBITMAP_TYPE_ANY); my $h = $img->GetHeight; my $w = $img->GetWidth; $img->Rescale($w-150, $h-50); my $bmp = Wx::Bitmap->new($img); $sb = Wx::StaticBitmap->new($panel, -1, $bmp);

      Update2: Fixed a typo.

      Update3: Seems you may want to scale from the original Wx::Image each time to avoid compounding any scaling artifacts/losses of resolution.


      There's never enough time to do it right, but always enough time to do it over...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1031116]
[Corion]: My household somewhat becomes a Dell stronghold since I threw out HP for not wanting to scan documents because of an empty ink cartridge ;)

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (11)
As of 2017-03-27 13:20 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (320 votes). Check out past polls.