1 Reply Latest reply: Oct 14, 2012 1:05 PM by shakir.gusaroff RSS

    Save the image after applying effect

    962357
      An Image is displayed in ImageView.
      User applies effects to the image.
      Now I want to save the modified image to disk.

      ImageView.getImage() method returns the original image. How can the image that has effects applied be retrieved and saved?

      Thanks for help!
        • 1. Re: Save the image after applying effect
          shakir.gusaroff
          Hi. Here is an example of how to save an image after applying a drop shadow.
             import java.io.File;
          import javafx.application.Application;
          import javafx.embed.swing.SwingFXUtils;
          import javafx.scene.*;
          import javafx.scene.image.*;
          import javafx.scene.layout.StackPane;
          import javafx.stage.*;
          import javax.imageio.ImageIO;
          import java.io.*;
          import javafx.scene.effect.DropShadow;
          import javafx.scene.paint.Color;
          
          
          
          public class PrintImage extends Application {
          
              @Override
              public void start(Stage primaryStage) {
          
                  DropShadow ds = new DropShadow();
                  ds.setOffsetY(3.0);
                  ds.setOffsetX(3.0);
                  ds.setColor(Color.RED);
          
                  StackPane root = new StackPane();
                  Scene scene = new Scene(root, 700, 650);
                 
                  ImageView iv = new ImageView(new Image("http://i46.tinypic.com/6ga8v5.png"));
                  iv.setEffect(ds);
                  
                  WritableImage wim = new WritableImage(300, 250);
                  iv.snapshot(null, wim);
                  root.getChildren().add(iv);
          
                  primaryStage.setTitle("Hello World!");
                  primaryStage.setScene(scene);
                  primaryStage.show();
          
                  File file = new File("ImageWithEffect.png");
          
                  try {
                      ImageIO.write(SwingFXUtils.fromFXImage(wim, null), "png", file);
                  } catch (Exception s) {
                  }
              }
          
              public static void main(String[] args) {
                  launch(args);
              }
          
             
          }