|
From: Mark D. <mar...@zn...> - 2007-03-27 19:49:47
|
Vaughn Staples wrote:
> Mark,
>
> A new question concerning the canvas.
>
> With the drawing objects now complete, I need to save the canvas data as
> an image file ... such as a BMP, JPG, or GIF.
Hi Vaughn
Procedure below shows the basics. You just have to look up the Wx::FileDialog in the docs and in Wx::Demo to see about selecting filename / filetype.
Regards
Mark
sub evt_menu_file_save {
my ($self, $event) = @_;
$event->Skip(1); # allow event to be processed by further handlers
# have user select a file name and type
my $filename = 'c:/testimage.png';
my $imagetype = wxBITMAP_TYPE_PNG;
# get the device context we have drawn on
my $canvas = $self->get_control('Canvas');
my $dc = Wx::ClientDC->new( $canvas );
$canvas->PrepareDC( $dc );
#get the dimensions
my ( $width, $height ) = $dc->GetSizeWH();
# create a bitmap to draw on
my $bmp = Wx::Bitmap->new($width, $height, -1);
# select it into a memory dc
my $mdc = Wx::MemoryDC->new();
$mdc->SelectObject($bmp);
# copy the source dc to the memory dc
$mdc->Blit(0,0,$width,$height,$dc,0,0);
# get our bitmap back by selecting a null bitmap into the memory dc
$mdc->SelectObject(wxNullBitmap);
# create image out of bitmap and save
my $img = Wx::Image->new($bmp);
# $img->SaveFile($filename , $imagetype);
# lets scale it just for fun
my $scaled = $img->Scale(int($width / 2), int($height/2));
$scaled->SaveFile($filename , $imagetype);
}
|