Load Gdk::Pixbuf from string


#1

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.


#2

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)


#3

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


#4

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 :wink:

2009/1/8 Geoff Y. removed_email_address@domain.invalid