JavaFX:将窗格视图保存到图像 [英] JavaFX: Save view of pane to image
问题描述
假设我在 StackPane
上有N层,我在 StackPane
上进行了混合,绘图,图像,矩形等.我将如何保存结果?即在 StackPane
上我的屏幕上显示的内容.
Suppose I have N layers on a StackPane
, I do blendings, drawings, images, rectangles etc on the StackPane
. How would I go about saving the result? Ie what shows on my screen on the StackPane
.
通常,假设我有任何可绘制的JavaFX元素,我将如何将该特定元素绘制到图像文件.
In general, suppose I have any drawable JavaFX element, how would I go about drawing that specific element to an imagefile.
我知道swing会提供其 getGraphics()
以便直接绘制到图像的像素缓冲区,但是我找不到与 Node
或 Pane
.
I know swing would offer its getGraphics()
for drawing directly to the pixel buffer of an image but I cannot find anything similar for Node
or Pane
.
推荐答案
You can take a snapshot
of the Node
and save that to file
覆盖以下内容:使用JavaFX拍摄快照!
我倾向于使用类似以下的内容:
I tend to use something like the below:
public void captureAndSaveDisplay(){
FileChooser fileChooser = new FileChooser();
//Set extension filter
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("png files (*.png)", "*.png"));
//Prompt user to select a file
File file = fileChooser.showSaveDialog(null);
if(file != null){
try {
//Pad the capture area
WritableImage writableImage = new WritableImage((int)getWidth() + 20,
(int)getHeight() + 20);
snapshot(null, writableImage);
RenderedImage renderedImage = SwingFXUtils.fromFXImage(writableImage, null);
//Write the snapshot to the chosen file
ImageIO.write(renderedImage, "png", file);
} catch (IOException ex) { ex.printStackTrace(); }
}
}
这篇关于JavaFX:将窗格视图保存到图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!