如何保存JavaFX Canvas的高DPI快照 [英] How to save a high DPI snapshot of a JavaFX Canvas

查看:640
本文介绍了如何保存JavaFX Canvas的高DPI快照的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Canvas上创建了一个图像,该图像按比例缩小以便使用变换进行显示。它也在ScrollPane中,这意味着只有部分图像可见。

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.

我需要拍摄整个画布的快照并将其保存为高分辨率图片。当我使用Canvas.snapshot时,我在缩小后得到图像可见部分的可写图像。这会导致保存低分辨率的局部图像。

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);
}


推荐答案

我做了以下工作来获取Retina显示屏上的画布快照,其pixelScaleFactor为2.0。它对我有用。

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);     
}

这篇关于如何保存JavaFX Canvas的高DPI快照的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆