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

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Sat Mar 19 11:05:19 EDT 2011

You could use `message%' with the bitmap as its label:

 #lang racket/gui

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

 (define f (new frame% [label "Bitmap"]))
 (new message% [parent f] [label bitmap])
 (send f show #t)


At Sat, 19 Mar 2011 08:01:22 -0700, John Clements wrote:
> 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?
> 
> John
> 
> 
> #lang racket
> 
> (require racket/draw
>          mred)
> 
> ;; 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))
>     (super-new)))
> 
> ;; 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)



Posted on the dev mailing list.