Save JavaFX WritableImage - Java JavaFX

Java examples for JavaFX:Image

Description

Save JavaFX WritableImage

Demo Code


//package com.java2s;

import java.io.File;
import java.io.IOException;

import javafx.embed.swing.SwingFXUtils;

import javafx.scene.image.WritableImage;

import javafx.stage.FileChooser;
import javafx.stage.Stage;

import javax.imageio.ImageIO;

public class Main {

    private static void saveImageFile(WritableImage writableImage,
            Stage stage) throws IOException {
        FileChooser fileChooser = new FileChooser();

        FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter(
                "image files (*.png)", "*.png");
        fileChooser.getExtensionFilters().add(extFilter);

        File file = fileChooser.showSaveDialog(stage);

        if (file != null) {
            /**/*from ww w . j av a  2s.c  o  m*/
             * ?.
             */
            String fileName = file.getName();

            if (!fileName.toUpperCase().endsWith(".PNG")) {
                file = new File(file.getAbsolutePath() + ".png");
            }

            // PixelReader pixelReader = image.getPixelReader();
            // int width = (int) image.getWidth();
            // int height = (int) image.getHeight();
            // WritableImage writableImage = new WritableImage(pixelReader, width, height);

            ImageIO.write(SwingFXUtils.fromFXImage(writableImage, null),
                    "png", file);
        }
    }
}

Related Tutorials