# Render natively

**URL:** <https://www.ruby-forum.com/t/render-natively/148048>\
**Category:** wxRuby\
**Created:** [September 8, 2008, 5:40pm UTC](https://www.ruby-forum.com/t/render-natively/148048 "2008-09-08T17:40:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [September 8, 2008, 5:40pm UTC](https://www.ruby-forum.com/t/render-natively/148048/1 "2008-09-08T17:40:02Z")

</div>

I would like to render natively i.e. via a C dll that I have wrapped  
using SWIK.

So I want my c code to receive a buffer that it can render to, and then  
have WXruby display this memory buffer/bitmap.

Can anyone advise me what would be the best method for doing this, to  
save me having to find out the hard way.

Many thanks,  
Jim.

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [September 8, 2008, 9:11pm UTC](https://www.ruby-forum.com/t/render-natively/148048/2 "2008-09-08T21:11:11Z")

</div>

Jim flip wrote:

> I would like to render natively i.e. via a C dll that I have wrapped  
> using SWIK.
> 
> So I want my c code to receive a buffer that it can render to, and then  
> have WXruby display this memory buffer/bitmap.

A drawing context (whether an in-memory buffer, an on-screen canvas, or  
a printout) is represented by a DC [1] instance in wxRuby. MemoryDC is  
the specific class for in-memory buffers.

However the API and class is cross-platform and specific to WxRuby, so  
you’re not going to be able to pass this in directly to your dll. You  
don’t give a lot of info about the way your API works, but I’d think  
probably the easiest way would be to have your dll render to a ruby  
String in a recognised image format (wxRuby understands PNG, TIF, JPG,  
BMP, TGA) then wrap this in a ruby StringIO object.

Call Wx::Image.read [2] to load this image into an object, then do  
Wx::Bitmap.from\_image to convert it to a windows bitmap that can be  
rendered on screen. Lastly, simple draw this onto a window surface using  
its paint method. Have a look at the example in samples/drawing/image.rb  
to see the basics of writing an image to screen.

> Can anyone advise me what would be the best method for doing this, to  
> save me having to find out the hard way.

This should probably work, but there’s a few other options should this  
not be suitable. Also have a search through the archives as there have  
been a few threads about rendering images from external libraries (eg  
ImageMagick).

cheers  
alex

[1] [http://wxruby.rubyforge.org/doc/dc.html](http://wxruby.rubyforge.org/doc/dc.html)  
[2] [http://wxruby.rubyforge.org/doc/image.html#Image\_read](http://wxruby.rubyforge.org/doc/image.html#Image_read)

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [September 9, 2008, 11:29am UTC](https://www.ruby-forum.com/t/render-natively/148048/3 "2008-09-09T11:29:16Z")

</div>

Thanks for the pointers, I think I will try using a bitmap and it’s  
set\_data method as this should be simple to try out but may be a bit  
slow.

Thanks,  
Jim.

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [September 9, 2008, 11:42am UTC](https://www.ruby-forum.com/t/render-natively/148048/4 "2008-09-09T11:42:52Z")

</div>

Jim flip wrote:

> Thanks for the pointers, I think I will try using a bitmap and it’s  
> set\_data method as this should be simple to try out but may be a bit  
> slow.

If you can avoid composing or decomposing the image data in Ruby (using  
pack etc) this will be pretty fast - for example, if you can get your  
extension to emit image data in that format. Then it’s just a C-level  
memcpy going on.

Similarly Image.read should be fast if you can get your extension to  
produce a known image format. These wxRuby methods are not particularly  
far off the native C library functions in libtiff, libpng etc

alex
