Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Using wxMemoryDC to draw on a jpeg

by Steve_BZ (Hermit)
on Sep 27, 2009 at 01:26 UTC ( #797727=snippet: print w/ replies, xml ) Need Help??

Description: I found it hard to locate any easy examples of drawing on Bitmaps using wxPerl. Here is some working code that I put together to see if I could make it work. Just make sure you point the .jpg filename to one that exits on your machine, and you have wxPerl installed. Many thanks to Huub Peters for helping me with Mouse events and understanding DC.
#!/usr/bin/perl -w -- 

use Wx 0.15 qw[:allclasses];
use strict;
use warnings;

package MyFrame;

use Wx qw[:everything];
use base qw(Wx::Frame);
use strict;
our $gl_self;
sub new {
    my( $self, $parent, $id, $title, $pos, $size, $style, $name ) = @_
+;
    $parent = undef              unless defined $parent;
    $id     = wxID_ANY                 unless defined $id;
    $title  = ""                 unless defined $title;
    $pos    = wxDefaultPosition  unless defined $pos;
    $size   = wxDefaultSize      unless defined $size;
    $name   = ""                 unless defined $name;

    $style = wxDEFAULT_FRAME_STYLE unless defined $style;

    $self = $self->SUPER::new( $parent, $id, $title, $pos, $size, $sty
+le, $name );
    $self->SetTitle("Drawing on image");

    $gl_self= $self;    # Set Global variable for use in EVT handler
    
# Image for drawing on
    $self->{image1} = Wx::Image->new( 'C:\insert_your_jpeg.jpg',  wxBI
+TMAP_TYPE_ANY, -1 );
    $self->{Loc_Photo_Bmp} = Wx::Bitmap->new( $self->{image1} ) ;
    $self->{bitmap_1} = Wx::StaticBitmap->new( $self, wxID_ANY, $self-
+>{Loc_Photo_Bmp});
    
# Button to draw image
    
    use Wx::Event qw( EVT_LEFT_UP );
    $self->{bitmap_1}->SetCursor(wxCROSS_CURSOR);
    $self->{bitmap_1}->Connect( wxID_ANY,wxID_ANY,wxEVT_LEFT_UP, \&on_
+button );
#
#    Sizer
#
    $self->{sizer_1} = Wx::BoxSizer->new(wxVERTICAL);
    $self->{sizer_1}->Add($self->{bitmap_1}, 0, 0, 0);
    $self->SetSizer($self->{sizer_1});
    $self->{sizer_1}->Fit($self);
    $self->Layout();
    
    return $self;
}
sub on_button{

        my ($self, $event) = @_;
    # select it into a memory dc
       if (defined $gl_self->{Loc_Photo_Bmp}){
        my $mdc = Wx::MemoryDC->new();
        $mdc->SelectObject($gl_self->{Loc_Photo_Bmp});
        my $pen = Wx::Pen->new( Wx::Colour->new(255,255,255), 3, wxSOL
+ID);
        $mdc->SetPen( $pen );
        $mdc->SetBrush( wxTRANSPARENT_BRUSH );
# Determine mouse event
#        my $m= Wx::MouseEvent->new($event);
        my $r = 50;
        my $x=$event->GetX();
        my $y=$event->GetY();
# Draw circle round mouse event
        $mdc->DrawCircle( $x, $y, $r );
        $mdc->SelectObject(wxNullBitmap); # deselect the bitmap out of
+ the DC
        $gl_self->{bitmap_1} ->SetBitmap($gl_self->{Loc_Photo_Bmp});
               $gl_self->{bitmap_1}->SetCursor(wxSTANDARD_CURSOR);
        $gl_self->{bitmap_1}->Disconnect( wxID_ANY,wxID_ANY,wxEVT_LEFT
+_DOWN );
        $gl_self->{bitmap_1}->Disconnect( wxID_ANY,wxID_ANY,wxEVT_LEFT
+_UP );
        }

    $event->Skip() ;
    return $self;
}

1;


package main;

unless(caller){
    local *Wx::App::OnInit = sub{1};
    my $app = Wx::App->new();
    Wx::InitAllImageHandlers();

    my $frame_1 = MyFrame->new();

    $app->SetTopWindow($frame_1);
    $frame_1->Show(1);
    $app->MainLoop();
}

Comment on Using wxMemoryDC to draw on a jpeg
Download Code

Back to Snippets Section

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://797727]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (14)
As of 2014-12-19 17:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (90 votes), past polls