当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Save the background image of a rectangle using file dialog in javafx

问题描述:

I have two rectangles in my scene .Then I set a background in those rectangles using setFill() and make some changes.How do I save those images to disk using saveDialog ? Here is my save function -

 FileChooser fileChooser = new FileChooser();

fileChooser.getExtensionFilters().add(new ExtensionFilter("Image Files", "*.png", "*.jpg", "*.gif"));

fileChooser.setTitle("Save Image");

File file = fileChooser.showSaveDialog(window);

rect2.getFill();

SaveFile(file);

网友答案:

You can create a javafx.scene.image.Image from any node by calling snapshot(...) on the node:

Image img = rect2.snapshot(null, null);

The javax.imageio.ImageIO class has methods for saving images in standard formats, but only works with AWT images, so you need to convert it first:

BufferedImage bImg = SwingFXUtils.fromFXImage(img, null);
String format = file.getName().substring(file.getName().lastIndexOf(".")+1);
ImageIO.write(bImg, format, file);
分享给朋友:
您可能感兴趣的文章:
随机阅读: