使用以下方法:
@FXML
public void exportSideImage() {
File imageLocation = ImageExport.display();
WritableImage image = SideView.getInstance().snapshot(null, null);
BufferedImage bImage = SwingFXUtils.fromFXImage(image, null);
try {
ImageIO.write(bImage, getFileExtension(imageLocation), imageLocation);
} catch (IOException e) {
e.printStackTrace();
}
}
private String getFileExtension(File file) {
String name = file.getName();
int lastIndexOf = name.lastIndexOf(".");
return name.substring(lastIndexOf + 1);
}
我的程序仅在指定了“ png”图像时才写入图像。我已经在多个平台(Linux,Windows)上对此进行了测试,但是问题仍然存在。有人知道这是怎么回事吗?
我建议您检查ImageIO.write中的结果代码,它可以报告为false,还可以打印文件扩展名以确认它是其中之一:
ImageIO.getWriterFileSuffixes()