JavaFX 2.0+ WebView /WebEngine render web page to an image


I'm looking for a way to load up a page and save the rendering as an image just as you would do with CutyCapt (QT + webkit EXE to do just that).

At the moment and without JavaFX, I do it by calling an external process from java and rendering to file than loading that file into an ImageBuffer... Neither very optimized nor practical and even less cross platform...

Using JavaFX2+ I tried playing with the WebView & WebEngine:

public class WebComponentTrial extends Application {
    private Scene scene;

    public void start(final Stage primaryStage) throws Exception {
        primaryStage.setTitle("Web View");
        final Browser browser = new Browser();
        scene = new Scene(browser, 1180, 800, Color.web("#666970"));

    public static void main(final String[] args) {
class Browser extends Region {
    static { // use system proxy settings when standalone application
    // System.setProperty("", "true");

    final WebView browser = new WebView();
    final WebEngine webEngine = browser.getEngine();

    public Browser() {

    protected void layoutChildren() {
        final double w = getWidth();
        final double h = getHeight();
        layoutInArea(browser, 0, 0, w, h, 0, HPos.CENTER, VPos.CENTER);

    protected double computePrefWidth(final double height) {
        return 800;

    protected double computePrefHeight(final double width) {
        return 600;

There is a deprecated method : renderToImage in Scene (see links below) that would do something that comes close and with which I'd might be able to work but it is deprecated... It being deprecated in JavaFX seems to mean that there is no javadoc advertising the replacement method and because I don't have access to the code, I cannot see how it was done...

Here are a couple of sites where I found some information but nothing to render a webpage to an image:

canvasImage and saveImage(canvasImage, fc.getSelectedFile()) from this one :

10/17/2011 4:16:22 PM

Accepted Answer

I have done this by launching JavaFX WebView on a Swing JFrame and JFXPanel. And then I use the paint() method on JFXPanel once the WebEngine status is SUCCEEDED.

You may follow this tutorial to make a WebView: Integrating JavaFX into Swing Applications

The code below demonstrate how I capture the rendered screen from JFXPanel.

public static void main(String args[]) {
    jFrame = new JFrame("Demo Browser");
    jfxPanel = new JFXPanel();

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            Platform.runLater(new Runnable() {
                public void run() {
                    browser = new FXBrowser();
                    jFrame.setSize((int) browser.getWebView().getWidth(), (int) browser.getWebView().getHeight());

                            new ChangeListener() {
                                public void changed(ObservableValue observable,
                                                    Object oldValue, Object newValue) {
                                    State oldState = (State) oldValue;
                                    State newState = (State) newValue;
                                    if (State.SUCCEEDED == newValue) {

private static void captureView() {
    BufferedImage bi = new BufferedImage(jfxPanel.getWidth(), jfxPanel.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics graphics = bi.createGraphics();
    try {
        ImageIO.write(bi, "PNG", new File("demo.png"));
    } catch (IOException e) {
8/23/2017 3:35:38 PM

For JavaFX 2.2 users there is a much more clean and elegant solution based on JavaFX Node snapshot. You can take a look to JavaFX node documentation at:

Here is an example of taking a capture from a WebView Node (as webView)

   File destFile = new File("test.png");
   WritableImage snapshot = webView.snapshot(new SnapshotParameters(), null);
   RenderedImage renderedImage = SwingFXUtils.fromFXImage(snapshot, null);
   try {
       ImageIO.write(renderedImage, "png", destFile);
   } catch (IOException ex) {
       Logger.getLogger(GoogleMap.class.getName()).log(Level.SEVERE, null, ex);

I had no problems with this solution, and the webview is rendered perfectly in the PNG according to the node size. Any JavaFx node can be rendered and saved to a file with this method.

Hope this help!

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow