File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes How to get byte[] from the Image? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to get byte[] from the Image?" Watch "How to get byte[] from the Image?" New topic

How to get byte[] from the Image?

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Can anyone give me a hint on how to a get a byte[] array or InputStream from the Image/Icon object. Need to store an image that doesn't come from the filesytem in the DB's LONG RAW field. I can get int[] with PixelGrabber class, but how does one convert it to byte[]? thanks...
Thomas Suer
Ranch Hand

Joined: Sep 03, 2001
Posts: 50
Please try this:

Good luck
Eric Hoskland

Joined: Oct 12, 2000
Posts: 29
I am having a similar problem:
I want to convert an java.awt.image into a byte array NOT into an int array. Shouldn't there be a way to specify a custom colorspace and colormodel to pixelgrabber so that it grabs the pixels and puts them into a byte array opposed to a int array??? Or is this just not possible
The image ONLY exists as a java.awt.image NEVER as anything else so there isn't a prior step that I can intercept (which is how the example above does it)
Martin Thorpe
Ranch Hand

Joined: May 25, 2004
Posts: 45
ok this is the sort of problem I am having too. I understand all of the above method apart from the baseClass stuff!?!?! can anyone explain that to me? How I should call this method?

thanks for taking the time to read.

Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
The Class argument is the class whose class loader will be asked to locate the file (see getResource Method Detail in Class api). If it is simply the enclosing class you can use the keyword this. If called from a static context you will need to use the MyClass.class form of class reference. If called from within an inner class you can use a member variable reference created at construction.

See the tutorial page How to Use Icons, especialy the section Loading Images Using getResource.
Ben Hur

Joined: Mar 07, 2005
Posts: 9
Why not just write the image out using the javax.imageio.ImageIO which has the following static method:

You're image does get encoded into the format you specify (i.e. jpeg or png for example), which does seem to be a good side-effect.

Take a peek at the
Java Almanac.
I agree. Here's the link:
subject: How to get byte[] from the Image?
It's not a secret anymore!