[racket-dev] nice way to show a bitmap in a frame?

From: John Clements (clements at brinckerhoff.org)
Date: Sat Mar 19 11:01:22 EDT 2011

Someone on stack overflow just asked how to draw a bitmap to the screen, and I couldn't think of an easier way than the following code.  Am I missing something obvious?  An existing bitmap-canvas% class, or something like it?


#lang racket

(require racket/draw

;; define a canvas that displays a bitmap when its on-paint
;; method is called
(define bitmap-canvas%
  (class canvas%
    (init-field [bitmap #f])
    (inherit get-dc)
    (define/override (on-paint)
      (send (get-dc) draw-bitmap bitmap 0 0))

;; load the bitmap
(define bitmap (read-bitmap "/tmp/red-arrow.bmp"))

;; create a new frame (top-level window)
(define f (new frame% [label "foo"] [width 100] [height 100]))

;; create a canvas
(define the-canvas (new bitmap-canvas% [parent f] [bitmap bitmap]))

;; show the canvas
(send f show #t)

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4669 bytes
Desc: not available
URL: <http://lists.racket-lang.org/dev/archive/attachments/20110319/9e9ecbe4/attachment.p7s>

Posted on the dev mailing list.