cant convert bmp Image to byte array
807588Jul 30 2009 — edited Jul 30 2009i 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.