Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to best serialize a java.awt.Image?

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.)

like image 933
Epaga Avatar asked Sep 26 '08 14:09

Epaga


People also ask

How do you serialize an image?

You can't serialize an Image . It is typically "hooked into" the sender's graphics environment in ways that serialization is not able to deal with. What you need to do is to mark the img field as transient . The net effect will be that the receiver sees null as the value.

How do you serialize a model in Java?

To serialize an object means to convert its state to a byte stream so that the byte stream can be reverted back into a copy of the object. A Java object is serializable if its class or any of its superclasses implements either the java. io. Serializable interface or its subinterface, java.

Which class should be used to serialize Java objects?

The ObjectOutputStream class contains writeObject() method for serializing an Object.


1 Answers

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

like image 149
pfranza Avatar answered Oct 01 '22 03:10

pfranza