Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Using wxMemoryDC to draw on a jpeg

by Steve_BZ (Chaplain)
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
    $self->{Loc_Photo_Bmp} = Wx::Bitmap->new( $self->{image1} ) ;
    $self->{bitmap_1} = Wx::StaticBitmap->new( $self, wxID_ANY, $self-
# Button to draw image
    use Wx::Event qw( EVT_LEFT_UP );
    $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);
    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();
        my $pen = Wx::Pen->new( Wx::Colour->new(255,255,255), 3, wxSOL
        $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}->Disconnect( wxID_ANY,wxID_ANY,wxEVT_LEFT
+_DOWN );
        $gl_self->{bitmap_1}->Disconnect( wxID_ANY,wxID_ANY,wxEVT_LEFT
+_UP );

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


package main;

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

    my $frame_1 = MyFrame->new();

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2023-02-07 19:04 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (40 votes). Check out past polls.