# wxRuby: MemoryDC problems

**URL:** <https://www.ruby-forum.com/t/wxruby-memorydc-problems/116286>\
**Category:** wxRuby\
**Created:** [October 4, 2007, 5:10am UTC](https://www.ruby-forum.com/t/wxruby-memorydc-problems/116286 "2007-10-04T05:10:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jay\_McGavren](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e79b87/32.png) [@Jay\_McGavren](https://www.ruby-forum.com/u/Jay_McGavren)\
**Post date:** [October 4, 2007, 5:10am UTC](https://www.ruby-forum.com/t/wxruby-memorydc-problems/116286/1 "2007-10-04T05:10:21Z")

</div>

I’m trying to put together a quick test of buffered animation with  
WxRuby, but I’m encountering a fair amount of trouble… I’m drawing  
to a Wx::Bitmap first, then using Wx::DC#draw\_bitmap to copy it to a  
Wx::Window. But I get the following error:

C:/ruby/lib/ruby/gems/1.8/gems/wxruby-1.9.1-i386-mswin32/lib/wx/classes/bitmap.rb:18:in  
`draw': uninitialized constant Wxruby2::Bitmap::MemoryDC (NameError) from dc_draw_line.rb:30:in`on\_init’  
…

What am I doing wrong? Is MemoryDC not implemented yet in WxRuby  
1.9.1? It’s in the documentation, but I know large chunks of that are  
auto-generated…

My code is below. Any advice would be most appreciated!

-Jay

gems\_loaded = false  
begin  
require ‘wx’  
rescue LoadError  
if gems\_loaded == false  
require ‘rubygems’  
gems\_loaded = true  
retry  
else  
raise  
end  
end

class MyApp \< Wx::App  
def on\_init  
#Create the top window.  
frame = Wx::Frame.new(nil, :size =\> [300, 300])  
#Create a panel within it.  
view = Wx::Window.new(frame)

```
    buffer = Wx::Bitmap.new(300, 300)

    @x = 0

#Set up a handler for paint events.
view.evt_paint do |event|
  #Wx::Window#paint takes a block.
  #This will later be called with a device context to paint on.
  view.paint do |dc|
    buffer.draw do |buffer_dc|
      buffer_dc.clear
      buffer_dc.pen = Wx::Pen.new(Wx::Colour.new(128, 128, 128), 5)
      buffer_dc.draw_line(@x, 0, @x + 100, 100)
    end
    dc.draw_bitmap(buffer, 0, 0, false)
  end
  @x += 1
end
#Show the window.
frame.show
return true

```

end  
end

app = MyApp.new

app.main\_loop

---

<div class="post-metadata">

**Author:** ![Jay\_McGavren](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e79b87/32.png) [@Jay\_McGavren](https://www.ruby-forum.com/u/Jay_McGavren)\
**Post date:** [October 8, 2007, 10:46am UTC](https://www.ruby-forum.com/t/wxruby-memorydc-problems/116286/2 "2007-10-08T10:46:13Z")

</div>

Hi Jay

sorry for the delay in replying.

Jay McGavren wrote:

> I’m drawing to a Wx::Bitmap first, then using Wx::DC#draw\_bitmap to copy it to a  
> Wx::Window. But I get the following error:
> 
> C:/ruby/lib/ruby/gems/1.8/gems/wxruby-1.9.1-i386-mswin32/lib/wx/classes/bitmap.rb:18:in  
> `draw': uninitialized constant Wxruby2::Bitmap::MemoryDC (NameError) from dc_draw_line.rb:30:in`on\_init’

It’s a bug - thanks for the report. It’s now fixed in SVN HEAD for  
version 1.9.2, but you can work around it for now by adding the correct  
definition of Bitmap#draw at the top of your script:

class Wx::Bitmap  
def draw  
dc = Wx::MemoryDC.new  
dc.select\_object(self)  
yield dc  
dc.select\_object( Wx::NULL\_BITMAP )  
end  
end

cheers  
alex
