How can i export an image to a jpg file? | Sololearn: Learn to code for FREE!

+1

How can i export an image to a jpg file?

I have converted a canvas into an image, but I can't find how to export it, I use java fx

1 Answer

New Answer

+2

Try this: import javax.imageio.ImageIO; import javafx.scene.image.WritableImage; import javafx.embed.swing.SwingFXUtils; import java.io.File; .... File file = new File(directoryPath+"test.jpg"); WritableImage writableimage = yourCanvas.snapshot(null); /* need to remove the alpha channel from the image. It's a bug. Instead of solving it, the library refuses to create a image without the alpha channel. Such as an jpg/JPEG file. BufferedImage jpgImage = new BufferedImage((int)writableimage.getWidth(), (int)writableimage.getHeight(), BufferedImage.TYPE_INT_RGB); ImageIO.write(SwingFXUtils.fromFXImage(jpgImage, null), "jpg", file);