Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to save a scene as image?

user753838-OracleApr 9 2015 — edited Apr 10 2015

Hello everyone,

I am new in JavaFX, I download/installed JDK8 with NB.

I am trying to save a scene as image (png or jpg), but the result png is blank. Wondering why?

Can any expert point out what is wrong with my simple code?

Thanks!!!

Here is the Java simple code:

----------------------------------------------------------------------------

import java.io.File;

import static javafx.application.Application.launch;

import javafx.embed.swing.SwingFXUtils;

import javafx.print.PrinterJob;

import javafx.scene.Scene;

import javafx.scene.SnapshotParameters;

import javafx.scene.image.WritableImage;

import javax.imageio.ImageIO;

public class WebViewTest2 extends javafx.application.Application {

   

    private Scene scene = null;

   

    @Override

    public void start(javafx.stage.Stage primaryStage) {

        try

        {

        String myURI = "file:///C:\\PSB\\JavaFXApplication1\\src\\hello2.html";

         

        // create WebView with specified local content

        final javafx.scene.web.WebView myBrowser = new javafx.scene.web.WebView();

        myBrowser.getEngine().load(myURI);

        myBrowser.setZoom(javafx.stage.Screen.getPrimary().getDpi() / 96);

        primaryStage.setTitle("HTML Hello");

        Scene myScene = new javafx.scene.Scene(myBrowser, 1100, 820);

        primaryStage.setScene(myScene);

        primaryStage.show();

        WritableImage mySnapShot = myScene.snapshot(null);

        File outImageFile = new File("C:\\PSB\\JavaFXApplication1\\src\\test11.png");

        ImageIO.write(SwingFXUtils.fromFXImage(mySnapShot, null), "png", outImageFile);

       

       

        }

        catch (Exception e)

        {

           

        }

       

       

    }

    public static void main(String[] args) {

        launch(args);       

    }

}

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 8 2015
Added on Apr 9 2015
7 comments
2,681 views