I'm trying to take a snapshot of a custom pane (which I placed at the center of a BorderPane), but what I get each time is a blank PNG image. I've tried the same code with a button and it's snapshot is taken and saved successfully. I've checked the bounds and I noticed the bounds of the pane have maximum values (both the bounds in parent and bounds in local):
[minX:-9.9999997952E10, minY:-9.9999997952E10, minZ:0.0, width:1.99999995904E11, height:1.99999995904E11, depth:0.0, maxX:9.9999997952E10, maxY:9.9999997952E10, maxZ:0.0]
I think the cause for that is that the pane's layout is set so it fills the center space of the BorderPane. I also found the following in the documentation which supports my suspicion:
A pane's unbounded maximum width and height are an indication to the parent that it may be resized beyond its preferred size to fill whatever space is assigned to it.
This is the relevant code for taking a snapshot:
final WritableImage SNAPSHOT = mNodeToExport.snapshot(new SnapshotParameters(), null);
final File FILE = new File(mPathTextField.getText());
try {
ImageIO.write(SwingFXUtils.fromFXImage(SNAPSHOT, null), "png", FILE);
return FILE;
} catch (IOException exception) {
System.err.println("Error while exporting image of logicboard: " + exception.getMessage());
return null;
}
The contents of the snapshot right after creating it are: debugging image which I think is pretty odd since it's width and height values should be way bigger than 1.
I've tried taking the snapshot of child nodes of the pane I'm trying to export and it works fine for the child nodes which have "non-infinite" bounds but returns the same image for the child node that has "infinite" bounds.
If the snapshot isn't taken correctly because of the "infinite" bound values, how can I temporarily change the pane's bounds for the snapshot? If the bounds are not the problem, then what might the problem be?
You can apparently pass a Rectangle2D that serves as the viewport for the snapshot. So the following does the job:
SnapshotParameters params = new SnapshotParameters();
params.setViewport(mLogicBoardView.getViewPort());
final WritableImage SNAPSHOT = mLogicBoardView.snapshot(params, null);
final File FILE = new File(mPathTextField.getText());
try {
ImageIO.write(SwingFXUtils.fromFXImage(SNAPSHOT, null), "png", FILE);
return FILE;
} catch (IOException exception) {
System.err.println("Error while exporting image of logicboard: " + exception.getMessage());
return null;
}
where I calculate the viewport manually in the getViewPort() function.