views:

1868

answers:

3

I have a Serializable object which is supposed to hold a java.awt.Image as its member. How should I go about serializing it? (Edited from a not so clear first version, sorry.)

A: 

None that I know of. I believe you need to write your own serializer for it to basically save out the width, height and pixel values... Or write it out to the stream as a PNG or something

tim_yates
+4  A: 

ImageIcon implements Serializable and it can be used to wrap an Image class

http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/ImageIcon.html

pfranza
I could kiss you. Thanks!
Epaga
you're most welcome*kissing not required
pfranza
Just be aware:Warning: Serialized objects of this class will not be compatible with future Swing releases.
McDowell
+1  A: 

javax.swing.ImageIcon, as a part of Swing, does not guarantee to have compatible serialised form between versions. However, you can cheat and look at its readObject and writeObject code - find width and height, grab the pixels with PixelGrabber. I'm not entirely sure that covers the colour model correctly. The obvious alternative is to write a real image format with javax.imageio.

Tom Hawtin - tackline