Starting with the wxRuby sample: bitmap_image.rb
I changed one line to try to crop the image, using the resize method.
That produced the following error message:
gary@Z790:~/Ruby/wxExamples/drawing$ ruby bitmap_image-1.rb
bitmap_image-1.rb:28:in resize': Wrong type for wxSize parameter (TypeError) from bitmap_image-1.rb:28:in
initialize’
from bitmap_image-1.rb:90:in new' from bitmap_image-1.rb:90:in
on_init’
from
/var/lib/gems/1.8/gems/wxruby-2.0.1-x86-linux/lib/wx/classes/app.rb:16:in
main_loop' from /var/lib/gems/1.8/gems/wxruby-2.0.1-x86-linux/lib/wx/classes/app.rb:16:in
run’
from bitmap_image-1.rb:89
bitmap_image-1.rb:28: [BUG] Segmentation fault
ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]
Aborted
The only change that was made to the sample file is shown below,
marked with #–>
The computer is running Ubuntu 9.04 (fully up to date),
ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]
wxruby-2.0.1-x86-linux
My understanding of the error is that the numbers in the resize argument
are of the wrong type. Yet changing resize to scale works, using the
same arguments.
Please explain what I need to do to get this to work, in order to crop
an image.
Full program code follows:
wxRuby2 Sample Code. Copyright © 2004-2008 wxRuby development team
Freely reusable code: see SAMPLES-LICENSE.TXT for details
begin
require ‘rubygems’
rescue LoadError
end
require ‘wx’
Bitmap sample (rewritten by Chauk-Mean P.)
This sample demonstrates how to draw the same image in various forms
(original, mirrored, greyscaled and blurred).
This sample uses :
- Wx::Image, which allows a wide range of manipulations such as
rescaling
and writing to files.
- Wx::Bitmap, which is a platform-specific representation of an image.
This is the class that must be used to actually display an image.
class ImageFrame < Wx::Frame
def initialize
super(nil, :title => ‘Simple image demo’, :size => [600, 600])
# Create the various images from the bitmap file
img_file = File.join( File.dirname(__FILE__), 'ruby-logo.jpg')
@image = Wx::Image.new(img_file)
#–> The only change made to the sample file is on this line, unchanged
below:
@mirrored_image = Wx::Image.new(img_file).mirror
#–> The following line produces the error:
@mirrored_image = Wx::Image.new(img_file).resize(100, 100)
#–> When resize above is change to scale below, no error.
@mirrored_image = Wx::Image.new(img_file).scale(100, 100)
@greyscaled_image = Wx::Image.new(img_file).convert_to_greyscale
@blurred_image = Wx::Image.new(img_file).blur(15)
# Create the corresponding bitmaps
compute_bitmaps
# Set up event handling
evt_size :on_size
evt_idle :on_idle
evt_paint :on_paint
end
Create a bitmap for the specified image and size
def compute_bitmap image, width, height
rescaled_image = Wx::Image.new(image).rescale(width, height)
rescaled_image.to_bitmap
end
Create the bitmaps corresponding to the images and with half the
size of the frame
def compute_bitmaps
width = client_size.x / 2
height = client_size.y / 2
@bitmap1 = compute_bitmap(@image, width, height)
@bitmap2 = compute_bitmap(@mirrored_image, width, height)
@bitmap3 = compute_bitmap(@greyscaled_image, width, height)
@bitmap4 = compute_bitmap(@blurred_image, width, height)
@done = true
end
Note to recompute the bitmaps on a resize
def on_size(event)
@done = false
event.skip
end
Recompute the bitmaps if needed, then do a refresh
def on_idle
if not @done
compute_bitmaps
refresh
end
end
Paint the frame with the bitmaps
def on_paint
paint do | dc |
if @done
width = client_size.x / 2
height = client_size.y / 2
dc.draw_bitmap(@bitmap1, 0, 0, false)
dc.draw_bitmap(@bitmap2, width, 0, false)
dc.draw_bitmap(@bitmap3, 0, height, false)
dc.draw_bitmap(@bitmap4, width, height, false)
end
end
end
end
Wx::App.run do
ImageFrame.new.show
end