# How to put a background image on a panel

**URL:** <https://www.ruby-forum.com/t/how-to-put-a-background-image-on-a-panel/163604>\
**Category:** wxRuby\
**Created:** [March 23, 2009, 3:12am UTC](https://www.ruby-forum.com/t/how-to-put-a-background-image-on-a-panel/163604 "2009-03-23T03:12:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [March 23, 2009, 3:12am UTC](https://www.ruby-forum.com/t/how-to-put-a-background-image-on-a-panel/163604/1 "2009-03-23T03:12:31Z")

</div>

Well I’ve made a lot of progress on my app, but I’m stuck on figuring  
out how to add a bitmap. I have a notebook and a page panel. On this  
panel I want to put a backdrop image and scale it to the size of the  
panel. How do I do this? I looked at some of the samples related to  
bitmaps and I could not see how to apply that to my situation.

Thanks,  
T.

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [March 23, 2009, 9:59am UTC](https://www.ruby-forum.com/t/how-to-put-a-background-image-on-a-panel/163604/2 "2009-03-23T09:59:00Z")

</div>

See:  
[http://rubyforge.org/pipermail/wxruby-users/2009-January/004448.html](http://rubyforge.org/pipermail/wxruby-users/2009-January/004448.html)

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [March 23, 2009, 11:33am UTC](https://www.ruby-forum.com/t/how-to-put-a-background-image-on-a-panel/163604/3 "2009-03-23T11:33:31Z")

</div>

2009/3/23 Mario S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> See: [http://rubyforge.org/pipermail/wxruby-users/2009-January/004448.html](http://rubyforge.org/pipermail/wxruby-users/2009-January/004448.html)

Thanks. Though that only got me so far. I added:

class Backdrop \< Wx::Window  
def initialize(parent, image)  
@parent = parent  
@image = Wx::Image.new(image)  
super(parent)  
evt\_erase\_background :on\_erase\_background  
end

```
def on_erase_background(evt)
  size = @parent.get_client_size
  set_size(size)
  @image.scale(size.get_width, size.get_height)
  evt.dc.draw_bitmap(@image.to_bitmap, 0, 0, false)
end

```

end

And then added an instance of it to my panel’s boxsizer:

```
  file = (DIR + '/images/cover.jpg')
  image = Backdrop.new(cover_panel, file)
  cover_sizer.add(image)

```

It draws the image initially but it isn’t scaled for some reason and  
then when I resize the window it shrinks to a small square.

T.

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [March 23, 2009, 3:41pm UTC](https://www.ruby-forum.com/t/how-to-put-a-background-image-on-a-panel/163604/4 "2009-03-23T15:41:50Z")

</div>

Remove the set\_size from the on\_erase\_background(), you shouldn’t use  
set\_size() in on\_erase\_background().
