I'm trying to write a simple "Paint"-like JavaFX-Application. I draw on to a JavaFX.scene.canvas, this works quite well.
Now I want to save this canvas as a ".png" image. Works, but the transparent pixels where swapped with white ones.
How do I save transparent pixels, as transparent Pixels?
Here is how I save the canvas:
private void saveFile(){
FileChooser fc = new FileChooser();
fc.setInitialDirectory(new File("res/maps"));
fc.getExtensionFilters().add(new FileChooser.ExtensionFilter("PNG","*.png"));
fc.setTitle("Save Map");
File file = fc.showSaveDialog(primaryStage);
if(file != null){
WritableImage wi = new WritableImage((int)WIDTH,(int)HEIGHT);
try { ImageIO.write(SwingFXUtils.fromFXImage(canvas.snapshot(null,wi),null),"png",file);
} catch (IOException e) {
e.printStackTrace();
}
}
}