byte[] to Image, HELP

C

Cyril Mrazek

Hello,

I have a byte array, I get it from a Blob, such that if I write it
into a FileOutputStream, I get a file on the disk:



.... some SQL
Blob blob = rs.getBlob(1);
int c = (int)blob.length();
byte[] buf = blob.getBytes(1, c);
FileOutputStream fos = new FileOutputStream("sample.jpg");
fos.write(buf, 0, c);
....



and it works fine. Now I would like to generate an Image, or a
BufferedImage, from that buffer and I can't find a way to do it. Any
hint ?

Thanks

Cyril Mrazek
 
C

Cyril Mrazek

Image img = Toolkit.getDefaultToolkit().createImage(buf);


I've tried that. It throws this exception:

java.lang.ClassCastException: sun.awt.windows.WImage

I suppose that the buffer is not structured in the expected way. But
why is it possible to create a correct file with it ?????

C.M.

Cyril Mrazek said:
Hello,

I have a byte array, I get it from a Blob, such that if I write it
into a FileOutputStream, I get a file on the disk:



... some SQL
Blob blob = rs.getBlob(1);
int c = (int)blob.length();
byte[] buf = blob.getBytes(1, c);
FileOutputStream fos = new FileOutputStream("sample.jpg");
fos.write(buf, 0, c);
...



and it works fine. Now I would like to generate an Image, or a
BufferedImage, from that buffer and I can't find a way to do it. Any
hint ?

Thanks

Cyril Mrazek
 
A

ak

I have a byte array, I get it from a Blob, such that if I write it
into a FileOutputStream, I get a file on the disk:



... some SQL
Blob blob = rs.getBlob(1);
int c = (int)blob.length();
byte[] buf = blob.getBytes(1, c);
FileOutputStream fos = new FileOutputStream("sample.jpg");
fos.write(buf, 0, c);
...



and it works fine. Now I would like to generate an Image, or a
BufferedImage, from that buffer and I can't find a way to do it. Any
hint ?
Image img = Toolkit.getDefaultToolkit().createImage(buf);


I've tried that. It throws this exception:

java.lang.ClassCastException: sun.awt.windows.WImage

can your imaging programm read this image (sample.jpg)?
 
C

Cyril Mrazek

I have a byte array, I get it from a Blob, such that if I write it
into a FileOutputStream, I get a file on the disk:



... some SQL
Blob blob = rs.getBlob(1);
int c = (int)blob.length();
byte[] buf = blob.getBytes(1, c);
FileOutputStream fos = new FileOutputStream("sample.jpg");
fos.write(buf, 0, c);
...



and it works fine. Now I would like to generate an Image, or a
BufferedImage, from that buffer and I can't find a way to do it. Any
hint ?
Image img = Toolkit.getDefaultToolkit().createImage(buf);


I've tried that. It throws this exception:

java.lang.ClassCastException: sun.awt.windows.WImage

can your imaging programm read this image (sample.jpg)?

Yes, the file is absolutely correct.
CM
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top