# Please can I use sgl to draw to a bitmap

**URL:** https://racket.discourse.group/t/please-can-i-use-sgl-to-draw-to-a-bitmap/923
**Category:** General
**Created:** [April 25, 2022, 1:19pm UTC](https://racket.discourse.group/t/please-can-i-use-sgl-to-draw-to-a-bitmap/923 "2022-04-25T13:19:37Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![hendrikboom3](https://avatars.discourse-cdn.com/v4/letter/h/b5e925/32.png) [@hendrikboom3](https://racket.discourse.group/u/hendrikboom3)
#### Post date: [April 25, 2022, 1:19pm UTC](https://racket.discourse.group/t/please-can-i-use-sgl-to-draw-to-a-bitmap/923/1 "2022-04-25T13:19:37Z")

</div>

I am currently using sgl to draw to a window, and using the screenshot progrm to convert the picture to a .png file.  
Unfortunately, this seems to limut the png to screen size, and I need more pixels than that for my application.

So my guess is that I need to create an sgl-compatible opengl environment that draws to bitmap instead of a window, and then use the tools available to write that bitmap out as a .png file.

Trouble is, I don't know how to get a bitmap out of sgl.

Here's the function I use to create the window and call a function called drawer to draw into it.

```scheme
(define (environ drawer)
  #;(printf "in environ\n")
  (define my-canvas%
    (class* canvas% ()
      (inherit with-gl-context swap-gl-buffers)
      
      (define/override (on-paint) ; When is on-paint caled?
        (printf "on-paint ~s~n" (count))
        (with-gl-context
            (lambda ()
              #;(printf "call drawer~n")
              (drawer)
              #;(printf "returned from drawer~n")
              (swap-gl-buffers)
              )
          )
        )

      (define/override (on-size width height) ; Is this what is called when resizing the window with a mouse?
        (with-gl-context
            (lambda ()
              (resize width height)
              )
          )
        )
      
      (super-instantiate () (style '(gl))) ; What is this?
      )
    )
  #;(printf "before win~n")
  (define win (new frame% (label "OpenGl Test")
                               (min-width 600)
                               (min-height 600)))
  #;(printf "before my-canvas~n")
  (define gl (new my-canvas% (parent win)))
  #;(printf "before show~n")
  (send win show #t) ; presumably this runs the on-paint method.
  ; But why does it get called three times?
  #;(printf "out-environ~n")
  )

```

Presumably I need to set up a canvas% or a drawing context differently.  
And, of course make a bitmap of the right size. Documentation suggests there are different kinds of bitmaps, and I'm not sure either which to use.

-- hendrik

---

<div class="post-metadata">

### Author: ![ryanc](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/ryanc/32/71_2.png) [@ryanc](https://racket.discourse.group/u/ryanc)
#### Post date: [April 25, 2022, 3:44pm UTC](https://racket.discourse.group/t/please-can-i-use-sgl-to-draw-to-a-bitmap/923/2 "2022-04-25T15:44:12Z")

</div>

Based on the docs, I would expected something like the following to work:

```
(require racket/class racket/draw racket/gui)
(define b (make-gl-bitmap 400 400 (new gl-config%)))
(define dc (send b make-dc))
(define glc (send dc get-gl-context))
(send glc call-as-current drawer)
(send b save-file "out.png" 'png)

```

Does that work for you?

---

<div class="post-metadata">

### Author: ![hendrikboom3](https://avatars.discourse-cdn.com/v4/letter/h/b5e925/32.png) [@hendrikboom3](https://racket.discourse.group/u/hendrikboom3)
#### Post date: [April 25, 2022, 4:35pm UTC](https://racket.discourse.group/t/please-can-i-use-sgl-to-draw-to-a-bitmap/923/3 "2022-04-25T16:35:08Z")

</div>

It works beautifully. Thank you.

And it's a lot simpler than I expected from my trail through the documentation.

I get the general drift; now it's time to look all this up in the documentation and understand exactly how these classes are related.

Thank you again.

-- hendrik
