Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Displaying changing images in Wx:: produces flicker

by isync (Hermit)
on Feb 27, 2009 at 15:23 UTC ( [id://746906]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks,

I've got a small problem related to WxPerl: I am constructing a GUI that displays an image in a corner, on top of a WxPanel. Based on actions somewhere else in the app, the app switches the displayed image. And as it seems over the great variety in approaches to display an image, I haven't got it quite right.

$this->{thumb_panel} = Wx::Panel->new($this->{parent}, -1, wxDefaultPo +sition, [320, 240], wxSUNKEN_BORDER); $this->{thumb_sb} = Wx::StaticBitmap->new($this->{thumb_panel}, -1, Wx +::Bitmap->new("somethumb.jpg", wxBITMAP_TYPE_ANY));

Now, when I exchange the displayed image with
my $handler = Wx::JPEGHandler->new(); my $image = Wx::Image->new(); my $bmp; $handler->LoadFile( $image, $file ); $image->Rescale(320,240); $bmp = Wx::Bitmap->new($image); if( $bmp->Ok() ) { $this->{thumb_sb} = Wx::StaticBitmap->new($this->{thumb_panel}, -1 +, $bmp); }
I experience flickering on the exchange. The image disappears for a second, or I can see "the switch taking place" or it starts out as a smaller square and zooms in on the 320x240 size (on windows) or (on Linux) first a formerly displayed image is shown, then replaced by the current one... something like that.

The parent window, a notebook, has wxCLIP_CHILDREN set - no effect. So would anyone please look at the above code and tell me if I am doing something completely wrong here. Or tell me that it is likely that the problem lies elsewhere in my code.

(and yes, I do need the resize on the exchange, as some displayed images might not be of 320x240 size, same with the check, as images might be damaged...)

Replies are listed 'Best First'.
Re: Displaying changing images in Wx:: produces flicker
by zentara (Archbishop) on Feb 27, 2009 at 15:33 UTC
    I can see "the switch taking place" or it starts out as a smaller square and zooms in on the 320x240 size

    Thats no bug!! It's a cool graphics effect you get for free. :-)


    I'm not really a human, but I play one on earth My Petition to the Great Cosmic Conciousness
      Right... ;-)
Re: Displaying changing images in Wx:: produces flicker
by Anonymous Monk on Feb 27, 2009 at 16:15 UTC
    Its called a StaticBitmap... you really should provide some testable code.
      Although your hint with *Static*Bitmap could have been a bit more verbose, it actually pointed me into the right direction.

      The proper way of displaying images on a panel, that you intend to change, is by using a Wx::PaintDC class, right?

      What a bit of thinking about code can do...
      my $dc = Wx::PaintDC->new($this->{thumb_panel}); my $bmp = Wx::Bitmap->new(Wx::Image->new("thumb.jpg", wxBITMAP_TYPE_AN +Y)->Scale(320,240)); $dc->DrawBitmap($bmp, 0, 0, 1);

      Although I am still experiencing some flicker (on Linux, Win32 todo) the underlying routine is much cleaner now. Any further notes?
        Although your hint with *Static*Bitmap could have been a bit more verbose, it actually pointed me into the right direction.
        It was a shot in the dark. Without code to test, you're relying a wxWidgets expert, one who has seen it all (and has ESP), will stop by perlmonks. Those guys hang out over here :)

        Anyway, I can't make this flicker (needs somethumb.jpg, somethumb2.jpg in cwd).

        #!/usr/bin/perl -- use warnings; use strict; use Wx 0.86 qw(:everything); my $app = Wx::SimpleApp->new; my $f = Wx::Frame->new( undef, -1, "Testing Popup", [ -1, -1 ], [ 555, + 555 ] ); Wx::InitAllImageHandlers(); my $p = Wx::Panel->new( $f, -1, wxDefaultPosition, [ 320, 240 ], wxSUNKEN_BO +RDER ); $p->{b} = my $b = Wx::Button->new( $p, -1, "GO", wxDefaultPosition, [ 50, 20 ], 0 ); # identify somethumb.jpg # somethumb.jpg JPEG 105x78 105x78+0+0 8-bit DirectClass 4.24kb 0.000u + 0:01 # identify somethumb2.jpg # somethumb2.jpg JPEG 105x78 105x78+0+0 8-bit DirectClass 4.1kb $p->{im1} = Wx::Image->new( 'somethumb.jpg', wxBITMAP_TYPE_ANY, -1 ); $p->{im2} = Wx::Image->new( 'somethumb2.jpg', wxBITMAP_TYPE_ANY, -1 ); $p->{imc} = 'im1'; $p->{sbmp} = my $sbmp = Wx::StaticBitmap->new( $p, -1, Wx::Bitmap->new( $p->{im1} ) ); $p->{sbz} = my $sbz = Wx::StaticBoxSizer->new( Wx::StaticBox->new( $p, -1, 'Wx::StaticBoxS +izer' ), wxVERTICAL ); $sbz->Add( $b, 0, wxALIGN_RIGHT | wxALL, 5 ); $sbz->Add( $sbmp, 0, wxALIGN_RIGHT | wxALL, 5 ); $p->SetAutoLayout(1); $p->SetSizer($sbz); $p->Layout(); $sbz->Fit($f); $sbz->SetSizeHints($f); $f->CenterOnScreen; $f->Show(1); use Wx::Event 'EVT_BUTTON'; EVT_BUTTON( $p, -1, sub { my ( $p, $e ) = @_; if ( $p->{imc} eq 'im1' ) { $p->{imc} = 'im2'; } else { $p->{imc} = 'im1'; } my $image = $p->{ $p->{imc} }; $image->Rescale( 320, 240 ); $image = Wx::Bitmap->new($image); if ( $image->Ok() ) { $p->{sbmp}->Destroy; ## $p->{sbmp} = Wx::StaticBitmap->new( $p, -1, $image ); $p->{sbz}->Add( $p->{sbmp}, 0, wxALIGN_RIGHT | wxALL, 0 ); $p->Layout(); $p->Refresh(); } } ); $app->SetTopWindow($f); $app->MainLoop(); __END__
      maybe do a ->Refresh();

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (8)
As of 2024-04-23 12:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found