我正在尝试保存由BufferedImage形成的图像。我通过执行以下操作来获得BufferedImage
(BufferedImage) fg;
fg是我的jPanel图形的图像。我成功地保存了图像,直接将路径硬编码为以下内容:
ImageIO.write((BufferedImage)fg,"png",new File("C:\\Users\\Geiger\\Documents\\test.png"));
但是,当我尝试将JFileChooser添加到mix中时,保存的图像显示为空白,除了jPanel的背景色之外什么都没有。
我尝试使用JFileChooser的代码如下:
JFileChooser jfc = new JFileChooser();
int retVal = jfc.showSaveDialog(null);
if(retVal==JFileChooser.APPROVE_OPTION){
File f = jfc.getSelectedFile();
String test = f.getAbsolutePath();
ImageIO.write((BufferedImage)fg,"png",new File(test));
}
编辑:进一步澄清这个问题:问题不在于文件不显示,而在于使用JFileChooser对象时图形不显示在图像上。
当JFrame有一个鼠标按下事件时,我更新我的图像:
fg = jPanel2.createImage(jPanel2.getWidth(), jPanel2.getHeight());
发布于 2013-11-22 04:50:17
试着把这行代码放入我认为这就是你需要的:
ImageIO.write(buffer, "png", fileDialog.getSelectedFile());
希望这能有所帮助
发布于 2016-12-09 15:28:49
Graphics2D graphics2D = image.createGraphics();
scribblePane.paint(graphics2D);
使用这两行代码添加图形。
发布于 2019-04-18 09:01:47
这对我来说很有效
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(new FileNameExtensionFilter("*.png", "png"));
if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
ImageIO.write((BufferedImage) image, "png", new File(file.getAbsolutePath()));
} catch (IOException ex) {
System.out.println("Failed to save image!");
}
} else {
System.out.println("No file choosen!");
}
}
https://stackoverflow.com/questions/20129279
复制相似问题