Save ImageIcon to file

Hello all,

Could someone teach me to save and ImageIcon class to an file. I've tried ImageIO.write, but that doesn't accept an ImageIcon class. Tried to use BufferedImage, but that doesn't accept ImageIcon either.

ImageIcon icon = new ImageIcon(file.getAbsolutePath(), "Test");

int h = icon.getIconHeight();

int w = icon.getIconWidth();

int hnew, wnew;

if (h > w) {

hnew = jLabelPict.getHeight();

wnew = w / (h / jLabelPict.getHeight());

} else {

wnew = jLabelPict.getHeight();

hnew = h / (w / jLabelPict.getHeight());

}

Image img = icon.getImage().getScaledInstance(

wnew, hnew, Image.SCALE_FAST);

icon.setImage(img);

jLabelPict.setIcon(icon);

jLabelPict.setText("");

jTextField.setText(file.getAbsolutePath().toString());

.....

At this point the file should be saved.

.....

Many thanks & regards

[948 byte] By [BabyGurua] at [2007-11-27 7:32:07]
# 1

hope this can works (not tested):

ImageIcon icon = ...;

BufferedImage bimg = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_RGB);

bimg.getGraphics().drawImage(icon.getImage(), 0, 0, null);

ImageIO.write(bimg, "jpeg", file);

j_shadinataa at 2007-7-12 19:12:21 > top of Java-index,Java Essentials,Java Programming...