使用javafx节点Node自带的snapshot方法截图,然后通过流写入导出成图片。
代码如下:
WritableImage image = node.snapshot(new SnapshotParameters(), null);
try {
String url = "C:/Users/test.png";
ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", new File(url));
System.out.println("保存成功");
} catch (IOException ex) {
System.out.println("保存失败:" + ex.getMessage());
}
ps:该方法只截取界面显示的部分,未显示或未加载完成的部分截取不到。