Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

cant convert bmp Image to byte array

807588Jul 30 2009 — edited Jul 30 2009
i want to convert Image to byte array. I am using below code to get byte array from Image. The below coding is working fine for gif, jpg and png but not support bmp files.

ImageIcon imageIcon = new ImageIcon("/Users/mac/java.bmp");
Image img = imageIcon.getImage();

Image imageResize = img.getScaledInstance(200, 200, 0);
ImageIcon imageIconResize = new ImageIcon (imageResize);

int resizeWidth = imageIconResize.getIconWidth();
int resizeHeight = imageIconResize.getIconHeight();
BufferedImage bi = new BufferedImage(resizeWidth, resizeHeight,
BufferedImage.TYPE_INT_RGB);

JPanel p = new JPanel();
Graphics2D big = bi.createGraphics();
big.drawImage(imageResize, 0, 0, p);
ByteArrayOutputStream os = new ByteArrayOutputStream();

try {
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
encoder.encode(bi);
byte[] byteArray = os.toByteArray();
FileOutputStream fout = new FileOutputStream("/Users/mac/resize.bmp");
fout.write(byteArray);
}catch(Exception e) {

}

The above code produce IllegalArgumentException : Width (-1) and height (-1) cannot be <= 0.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 27 2009
Added on Jul 30 2009
3 comments
952 views