# Wxruby - painting in device context

**URL:** <https://www.ruby-forum.com/t/wxruby-painting-in-device-context/176197>\
**Category:** Ruby\
**Created:** [September 26, 2009, 3:15am UTC](https://www.ruby-forum.com/t/wxruby-painting-in-device-context/176197 "2009-09-26T03:15:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Haris\_BogdanoviS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a8b319/32.png) [@Haris\_BogdanoviS\_S](https://www.ruby-forum.com/u/Haris_BogdanoviS_S)\
**Post date:** [September 26, 2009, 3:15am UTC](https://www.ruby-forum.com/t/wxruby-painting-in-device-context/176197/1 "2009-09-26T03:15:45Z")

</div>

Hi.  
I’m trying to paint in DC context:

# ---------------------------------------------------

frame = Frame.new(nil, -1, “”)

frame.paint { |dc|

```
dc.draw_line(50, 50, 100, 100) # for

```

this I don’t see the line

```
dc.draw_bitmap(Bitmap.new('c:\img.bmp'),0,0) # and for this

```

program crashes

}

# -----------------------------------------------------------

Does anyone know why is this happening ?

Thanks.

---

<div class="post-metadata">

**Author:** ![Haris\_BogdanoviS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a8b319/32.png) [@Haris\_BogdanoviS\_S](https://www.ruby-forum.com/u/Haris_BogdanoviS_S)\
**Post date:** [September 26, 2009, 9:59am UTC](https://www.ruby-forum.com/t/wxruby-painting-in-device-context/176197/2 "2009-09-26T09:59:55Z")

</div>

Haris Bogdanoviï¿½ wrote:

> Hi.  
> I’m trying to paint in DC context:
> 
> # ---------------------------------------------------
> 
> frame = Frame.new(nil, -1, “”)
> 
> frame.paint { |dc|
> 
> ```
> dc.draw_line(50, 50, 100, 100) # for
> 
> ```
> 
> this I don’t see the line
> 
> ```
> dc.draw_bitmap(Bitmap.new('c:\img.bmp'),0,0) # and for this
> 
> ```
> 
> program crashes
> 
> }
> 
> # -----------------------------------------------------------
> 
> Does anyone know why is this happening ?
> 
> Thanks.

It appears to me that you cannot draw on a DC before the window is  
created. The following shows nothing with Ruby 1.9 on my Ubuntu Jaunty  
machine:

=======================================  
#!/usr/bin/env ruby  
#Encoding: UTF-8  
require “wx”

class GUI \< Wx::App  
include Wx

def on\_init  
@mainwindow = Frame.new(nil, -1, “Test”, DEFAULT\_POSITION,  
Size.new(400, 400))

```
@mainwindow.paint{|dc| dc.draw_line(0, 0, 100, 100)}

@mainwindow.show

```

end

end

# x = GUI.new x.main\_loop

But if you use the #paint method after the window has been created,  
everything works fine:

=======================================  
#!/usr/bin/env ruby  
#Encoding: UTF-8  
require “wx”

class GUI \< Wx::App  
include Wx

def on\_init  
@mainwindow = Frame.new(nil, -1, “Test”, DEFAULT\_POSITION,  
Size.new(400, 400))

```
Timer.after(2000){@mainwindow.paint{|dc| dc.draw_line(0, 0, 100, 

```

100)}}

```
@mainwindow.show

```

end

end

# x = GUI.new x.main\_loop

Marvin

---

<div class="post-metadata">

**Author:** ![Haris\_BogdanoviS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a8b319/32.png) [@Haris\_BogdanoviS\_S](https://www.ruby-forum.com/u/Haris_BogdanoviS_S)\
**Post date:** [September 26, 2009, 10:02am UTC](https://www.ruby-forum.com/t/wxruby-painting-in-device-context/176197/3 "2009-09-26T10:02:52Z")

</div>

Haris Bogdanoviï¿½ wrote:

> ```
> dc.draw_bitmap(Bitmap.new('c:\img.bmp'),0,0) # and for this
> 
> ```
> 
> program crashes  
> You may want to use a forward slash / instead of the backslash, even on  
> Windows. An alternative is to escape the backslash by duplicating it:  
> “C:/img.bmp” or “C:\img.bmp”

Marvin

---

<div class="post-metadata">

**Author:** ![Haris\_BogdanoviS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a8b319/32.png) [@Haris\_BogdanoviS\_S](https://www.ruby-forum.com/u/Haris_BogdanoviS_S)\
**Post date:** [September 26, 2009, 7:05pm UTC](https://www.ruby-forum.com/t/wxruby-painting-in-device-context/176197/4 "2009-09-26T19:05:44Z")

</div>

> frame.paint { |dc|

Hello Haris:

I haven’t been able to get a line drawn either but I know it’s possible.  
The WxRuby samples directory contains a files called bitmap.rb and  
graphics\_drawing.rb that draws bitmaps, text and rectangles.

Michael
