java - Saving a javafx.scene.canvas with transparent Pixels -
java - Saving a javafx.scene.canvas with transparent Pixels -
i'm trying write simple "paint"-like javafx-application. draw on javafx.scene.canvas, works quite well.
now want save canvas ".png" image. works, transparent pixels swapped white ones.
how save transparent pixels, transparent pixels?
here how save 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); seek { imageio.write(swingfxutils.fromfximage(canvas.snapshot(null,wi),null),"png",file); } grab (ioexception e) { e.printstacktrace(); } } }
the problem when snapshot canvas, first argument snapshot
null
, means default snapshotparameters used. in particular, entire destination image first filled snapshotparameter's fill value. since argument null, default fill value null, means fill value (see snapshotparameters.setfill) white.
to prepare this, create snapshotparameters object, set fill transparent, , utilize in phone call snapshot
:
snapshotparameters sp = new snapshotparameters(); sp.setfill(color.transparent); ... imageio.write(swingfxutils.fromfximage(canvas.snapshot(sp, wi), null), "png", file);
java javafx-8
Comments
Post a Comment