Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » How to serialize an SWT Image
How to serialize an SWT Image [message #508454] Mon, 18 January 2010 22:53 Go to next message
Kyle Neumeier is currently offline Kyle Neumeier
Messages: 3
Registered: July 2009
Junior Member
I have a model object which contains an SWT Image. I need to serialize the object, but the image does not implement the serializable interface. Does anyone know the best way to solve this problem?

I'm thinking I might be able to use the ImageLoader class along with the readObject and writeObject methods that the java serialization protocol uses. Thanks in advance.
Re: How to serialize an SWT Image [message #508459 is a reply to message #508454] Mon, 18 January 2010 23:27 Go to previous message
Kyle Neumeier is currently offline Kyle Neumeier
Messages: 3
Registered: July 2009
Junior Member
I should have played around a bit more before I asked the question, but here is how I did it in case anyone is interested.
	private void writeObject(ObjectOutputStream out) throws IOException {
		ImageLoader imageLoader = new ImageLoader();
		imageLoader.data = new ImageData[] { data };
		ByteArrayOutputStream stream = new ByteArrayOutputStream();
		imageLoader.save(stream, SWT.IMAGE_PNG);
		byte[] bytes = stream.toByteArray();
		out.writeInt(bytes.length);
		out.write(bytes);
	}

	private void readObject(ObjectInputStream in) throws IOException,
			ClassNotFoundException {
		int length = in.readInt();
		byte[] buffer = new byte[length];
		in.readFully(buffer);
		ImageLoader imageLoader = new ImageLoader();
		ByteArrayInputStream stream = new ByteArrayInputStream(buffer);
		ImageData[] data = imageLoader.load(stream);
		this.data = data[0];
	}


It doesn't seem to be very efficient, so if anyone has any suggestions to make it quicker, please let me know.
Previous Topic:Text selection color of StyledText
Next Topic:SWT painting performance
Goto Forum:
  


Current Time: Thu Oct 23 18:34:48 GMT 2014

Powered by FUDForum. Page generated in 0.05852 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software