Images and bitmaps
wxRuby3 has two distinct classes for image data: Wx::Image and Wx::Bitmap. Understanding the difference between them is essential before trying to display or manipulate images.
Wx::Image is a platform-independent image container. It holds raw pixel data, supports loading and saving in various formats, and provides transformation operations like scaling, rotation, and colour manipulation. It is the right class for image processing.
Wx::Bitmap is a platform-specific image optimised for display. It is what the DC drawing methods expect. You cannot scale or transform a bitmap directly — convert it to an image first, transform, then convert back.
The workflow is: load as Image → transform → convert to Bitmap → draw.
Loading images
|
|
Wx::Image.new detects the format from the file extension. Supported formats include JPEG, PNG, GIF, BMP, TIFF, and WebP.
Always check that loading succeeded before using the image:
|
|
Displaying images
Never use Wx::StaticBitmap for resizable image display. Its client_size reflects the bitmap dimensions rather than the available space, which means resizing the window has no effect. Use a plain Wx::Panel with evt_paint instead:
|
|
The third argument to draw_bitmap is use_mask — pass false for opaque images, true if the bitmap has a transparency mask.
Scaling images
Scale via Wx::Image#scale, then convert back to a bitmap:
|
|
Wx::IMAGE_QUALITY_HIGH uses bicubic interpolation — slower but produces much better results than the default nearest-neighbour. Always use it for display scaling.
Fit to a bounding box
To scale an image to fit within a box while preserving aspect ratio, take the minimum of the two scale factors:
|
|
This is the pattern used in every image thumbnail, photo browser, and gallery app.
Off-screen drawing with MemoryDC
Wx::MemoryDC.draw_on creates a DC that targets a bitmap rather than the screen. All the drawing methods from lesson 4.1 work identically. Use it to compose images programmatically — thumbnails, contact sheets, charts that need to be saved to file.
|
|
The bitmap is finalised when the block exits. The DC is only valid inside the block — the same constraint as the paint DC.
Saving images to file
Convert a bitmap to an image and call save_file:
|
|
Supported output formats include Wx::BITMAP_TYPE_PNG, Wx::BITMAP_TYPE_JPEG, and Wx::BITMAP_TYPE_BMP. PNG is recommended for graphics and screenshots; JPEG for photographs.
This works for both bitmaps loaded from files and bitmaps composed with MemoryDC.draw_on — they are identical from the DC’s perspective.
Resizable image display pattern
The complete pattern for a panel that displays an image and scales it to fit when resized:
|
|
This pattern is used in the Gallery Tiler and GPS Track Viewer in Module 6.
Performance note: Scaling inside the paint handler works well for occasional repaints, but scaling a large image on every resize event can make the UI feel sluggish. For better performance, cache the scaled bitmap and only rescale when the panel size changes meaningfully.
See image_demo.rb for the complete working demonstration including contact sheet composition and file saving.
What to take forward
Wx::Image— for loading, saving, and transforming.Wx::Bitmap— for display.- Scale via
img.scale(w, h, Wx::IMAGE_QUALITY_HIGH), thenWx::Bitmap.new(scaled) - Use
evt_paint+draw_bitmapfor resizable image display — neverWx::StaticBitmap Wx::MemoryDC.draw_on(bmp) { |dc| }for off-screen composition- Save with
bitmap.convert_to_image.save_file(path, Wx::BITMAP_TYPE_PNG) - Fit to a box:
scale = [box_w / img.width, box_h / img.height].min
Previous: Drawing with device contexts | Next: Rich text