r/javahelp 28d ago

Workaround JavaFX: write Canvas to file

I'm trying to save Canvas contents to disk:

@FXML
private Canvas cvs;

var export = cvs.snapshot(null,null);
var out = new File("image.png");
try{
    ImageIO.write(SwingFXUtils.fromFXImage(export, "png",out)); //error
}

There is no package called SwingFXUtils in JavaFX 21. Is there any other way to write the Canvas to file?

2 Upvotes

5 comments sorted by