Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save a high DPI snapshot of a JavaFX Canvas

I have created an image on a Canvas which is scaled down for display using a transformation. It is also in a ScrollPane which means only a part of the image is visible.

I need to take a snapshot of the entire canvas and save this as a high-resolution image. When I use Canvas.snapshot I get a Writable image of the visible part of the image after scaling down. This results in a low-res partial image being saved.

So how do I go about creating a snapshot which includes the entire canvas (not only the viewport of the scrollpane) and with the resolution before the transformation downwards?

I am not doing anything fancy currently, just this:

public WritableImage getPackageCanvasSnapshot()
{
    SnapshotParameters param = new SnapshotParameters();
    param.setDepthBuffer(true);
    return packageCanvas.snapshot(param, null);
}
like image 784
Cobusve Avatar asked Dec 19 '22 00:12

Cobusve


1 Answers

I did the following to get a canvas snapshot on a Retina display with a pixelScaleFactor of 2.0. It worked for me.

public static WritableImage pixelScaleAwareCanvasSnapshot(Canvas canvas, double pixelScale) {
    WritableImage writableImage = new WritableImage((int)Math.rint(pixelScale*canvas.getWidth()), (int)Math.rint(pixelScale*canvas.getHeight()));
    SnapshotParameters spa = new SnapshotParameters();
    spa.setTransform(Transform.scale(pixelScale, pixelScale));
    return canvas.snapshot(spa, writableImage);     
}
like image 124
mipa Avatar answered Feb 20 '23 19:02

mipa