# Load Gdk::Pixbuf from string

**URL:** <https://www.ruby-forum.com/t/load-gdk-pixbuf-from-string/157176>\
**Category:** Ruby-Gnome 2\
**Created:** [January 8, 2009, 5:05am UTC](https://www.ruby-forum.com/t/load-gdk-pixbuf-from-string/157176 "2009-01-08T05:05:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mathieu\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/73ab20/32.png) [@Mathieu\_B](https://www.ruby-forum.com/u/Mathieu_B)\
**Post date:** [January 8, 2009, 5:05am UTC](https://www.ruby-forum.com/t/load-gdk-pixbuf-from-string/157176/1 "2009-01-08T05:05:45Z")

</div>

Hi all,

I have a Ruby string containing an image. The format, width, height,  
bits  
per sample, rowstride and so on, are not known in advance.

If I save my string to a file and create a pixbuf with  
Gdk::Pixbuf.new(myfile), this works, but I would like to get my pixbuf  
without touching the hard disk. Is that possible?

Any help appreciated.

Thank you,  
Mathieu B.

---

<div class="post-metadata">

**Author:** ![Mathieu\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/73ab20/32.png) [@Mathieu\_B](https://www.ruby-forum.com/u/Mathieu_B)\
**Post date:** [January 8, 2009, 10:12am UTC](https://www.ruby-forum.com/t/load-gdk-pixbuf-from-string/157176/2 "2009-01-08T10:12:10Z")

</div>

this is how i did it:  
a=Array.new  
string.each\_byte{|v|a.push(v)}  
pixdata=Gdk::Pixdata.deserialize(a)  
pb=pixdata.to\_pixbuf(true)  
im=Gtk::Image.new(pb)

---

<div class="post-metadata">

**Author:** ![Mathieu\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/73ab20/32.png) [@Mathieu\_B](https://www.ruby-forum.com/u/Mathieu_B)\
**Post date:** [January 8, 2009, 11:34am UTC](https://www.ruby-forum.com/t/load-gdk-pixbuf-from-string/157176/3 "2009-01-08T11:34:51Z")

</div>

If it’s an encoded image (e.g. JPEG, PNG etc) rather than a serialized  
GdkPixdata, you’ll want to use Gdk::PixbufLoader

loader = Gdk::PixbufLoader.new  
loader.write(data)  
loader.close

pixbuf = loader.pixbuf

2009/1/8 Dobai-Pataky B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Mathieu\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/73ab20/32.png) [@Mathieu\_B](https://www.ruby-forum.com/u/Mathieu_B)\
**Post date:** [January 8, 2009, 1:41pm UTC](https://www.ruby-forum.com/t/load-gdk-pixbuf-from-string/157176/4 "2009-01-08T13:41:46Z")

</div>

Thank you to both of you for your quick reply. Yes, my string contains  
the  
dump of an encoded file. It’s not a string of pixels. The PixbufLoader  
solution made the trick! I should not work when I’m too tired, this  
makes me  
misread the documentation 😉

2009/1/8 Geoff Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
