转载自 JavaFX如何将WritableImage转换为Image
我正在做一些应用程序,制作截图LineChart
并将其保存为pdf,所以我不知道将WritableImage
(JavaFX 2.2)转换为Image
(iText lib)的平滑方式。
临时解决方案是
- 然后,制作快照
- 获得
WritableImage
从该快照 - 将图像写入png文件
- 打开图像并制作iText对象
Image
我想做一些更改:我不想将png
文件写入光盘,我只想将快照写入pdf
WritableImage wim = new WritableImage((int) lineChart.getWidth(),(int) lineChart.getHeight());Scene scena = primaryStage.getScene();scena.snapshot(wim);File fileA = new File("C://Graphs/chart.png");try {ImageIO.write(SwingFXUtils.fromFXImage(wim, null), "png", fileA);}catch (Exception s) {}pdfDocument.add(preface3);com.itextpdf.text.Image graph =com.itextpdf.text.Image.getInstance("C://Graphs/chart.png");pdfDocument.add((com.itextpdf.text.Element) graph);
用途:
ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();ImageIO.write( SwingFXUtils.fromFXImage( wim, null ), "png", byteOutput );com.itextpdf.text.Image graph;
graph = com.itextpdf.text.Image.getInstance( byteOutput.toByteArray() );
从画布中复制绘图,将复制到WritableImage,而不是从WritableImage获得图像
WritableImage wi = new WritableImage((int)gc.getCanvas().getWidth(), (int)gc.getCanvas().getHeight());gc.getCanvas().snapshot(null, wi); //Coping all that now in Canvas//gc is GraphicContext object from Canvas, it has drawing functionsBufferedImage bi =SwingFXUtils.fromFXImage((Image)wi, null); SwingFXUtils.toFXImage(bi, (WritableImage)image);