MemoryDC

From WxPerl Wiki
Jump to: navigation, search

Using wxMemoryDC to draw on a jpeg

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. Many thanks to HuubPeters 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, $style, $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_my_jpeg.jpg',  wxBITMAP_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, wxSOLID);
		$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();
}