希望这会更好,我已经尝试了很多次。
public void writeImage(String output, String fileName, BufferedImage img) throws IOException {
File file = new File(output + "\\HE\\" + fileName + ".bmp");
ImageIO.write(img, "bmp", file);
}
==================================================== ================================
如果您想在任何 JPanel 中使用此图像,那么这里是它的代码,它已经可以正常工作了,
import java.awt.BorderLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class ShowImage {
public ShowImage(final String filename) throws Exception {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame editorFrame = new JFrame("My Frame " +filename);
editorFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
BufferedImage image = null;
try {
image = ImageIO.read(new File(filename));
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
ImageIcon imageIcon = new ImageIcon(image);
JLabel jLabel = new JLabel();
jLabel.setIcon(imageIcon);
editorFrame.getContentPane().add(jLabel, BorderLayout.CENTER);
editorFrame.pack();
editorFrame.setLocationRelativeTo(null);
editorFrame.setVisible(true);
}
});
}
}