JavaFX Snapshot Scaling

If you try taking image snapshots of a JavaFX Node on a high-DPI system, you’ll find that the result is strangely blurry. This is an unfortunate side effect of the JavaFX DPI scaling introduced in Java SE 8u60. At resolutions greater than 120 DPI, JavaFX automatically treats all coordinates as abstract “layout pixels” with a resolution of 96 DPI, and later internally upscales rendering to the actual physical pixel density. That’s fundamentally a good mechanism: it ensures that you always get the best possible rendering resolution without affecting layout spacing.

This mechanism does cause trouble with snapshots, however. Snapshots transform a JavaFX Node which is usually a vector component (unlimited resolution) into a WritableImage – and that’s a bitmap component with a fixed limited resolution depending on its size. Where does that size come from? From the size of the Node whose snapshot is taken, of course – and that size is specified in layout pixels at 96 DPI. So that is the same resolution you get in your snapshot, even if your physical display density is two or four times higher. Result: blurry snapshots because the Node gets down-rendered to 96 DPI for the WritableImage.

Happily there’s a simple workaround. You don’t have to accept the default-sized image that is automatically created by Node.snapshot. You can supply your own WritableImage, with an arbitrarily upscaled size relative to the layout dimensions of the Node, and then apply a corresponding scaling transformation for the snapshot via SnapshotParameters.

Sample Program

I wrote a small sample program to demonstrate unscaled and scaled snapshots. The screenshot shows its output on my Windows 10 system with DPI scaling set to 200% = 192 DPI. The first row shows a sample Text node using Georgia. The second row shows the unacceptably blurry result of a snapshot taken with default parameters. Finally, the third row shows a snapshot scaled by a factor of four – indistinguishable from the original Text node.

The rightmost column of the top row shows the reported layout size of the original Text node. For the other two rows, this column shows the pixel dimensions of the bitmap images in the corresponding ImageView. As you can see, the unscaled snapshot directly takes its image size from the (rounded) layout size of the original Text node – hence the blurriness. The image size of the scaled ImageView equals the (rounded) layout size of the Text node multiplied by four, as expected.

The full code of the sample program appears below. I compiled and ran it using Java SE 8u121. Snapshot scaling is implemented by the ready-to-use method createScaledView.

Note that since we upscaled the snapshot image, we have to correspondingly downscale the ImageView via setFitWidth/Height, or it would appear at four times the size of the original Text node. This is because image sizes are interpreted as 96 DPI layout pixels by JavaFX. As far as I can tell, there is no way to specify a desired DPI resolution on either WritableImage or ImageView. So to have our image appear at the correct size, we must manually adjust the size of the ImageView.

Another noteworthy point is the hard-coded scaling factor of four. This should suffice for all current displays, but it would be nicer to have it dynamically calculated based on screen DPI. Sadly, reporting physical screen DPI is another feature that JavaFX does not currently support. I believe it’s planned for an upcoming release, though.