JavaFX 8带Alpha的画布快照

Jab*_*oyc 4 java javafx canvas

我目前正在开发一个绘画程序(类似于Gimp和Photoshop),为了做到这一点,我将需要图层。我创建了一个名为JImage的类,该类具有ArrayList<Canvas> layers和一些方法。

public Image toImage(){ //Returns the final image which is all its layers combined into one canvas and snapshotted.
    Canvas c = new Canvas(width, height); //width and height are determined in the constructor
    for(int i=layers.size()-1;i>=0;i--){
        Canvas currLayer = layers.get(i);
        c.getGraphicsContext2D().drawImage(currLayer.snapshot(new SnapshotParameters(), new WritableImage(width,height)));
    }
    return c.snapshot(new SnapshotParameters(), new WritableImage(width,height));
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,当您这样做时canvas.snapshot(SnapshotParameters,WritableImage),不包括Alpha层,并且背景始终为白色。这样可以防止我将其发送到没有丑陋白色背景的文件中。有什么办法可以从具有alpha层的多个画布中获取图像?我希望将JavaFX用于此解决方案,因此请在JavaFX的范围内提供解决方案。

jew*_*sea 8

在拍摄快照之前,将SnapshotParameters 的填充设置Color.TRANSPARENT

SnapshotParameters params = new SnapshotParameters();
params.setFill(Color.TRANSPARENT);
Image snapshot = currLayer.snapshot(params, null);
Run Code Online (Sandbox Code Playgroud)

从javadoc:

将填充设置为指定值。这用于在渲染节点之前填充要渲染的整个图像。值为null表示应使用白色填充。默认值为空。