JavaFX 8画布快照带有alpha通道

6

我目前正在开发一个绘画程序(类似于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));
}

我的问题是,当使用canvas.snapshot(SnapshotParameters,WritableImage)时,alpha层不被包含在内,背景总是白色的。这会导致我无法将其发送到文件中,因为它会有一个丑陋的白色背景。有没有办法从多个带有alpha层的画布中获取图像?我希望使用JavaFX解决此问题,请给出JavaFX范围内的解决方案。
1个回答

11

在进行快照之前,将您的SnapshotParameters的填充设置Color.TRANSPARENT

SnapshotParameters params = new SnapshotParameters();
params.setFill(Color.TRANSPARENT);
Image snapshot = currLayer.snapshot(params, null);

根据Javadoc:

设置填充为指定值。这用于在渲染节点之前填充整个正在呈现的图像。空值表示应该使用白色作为填充颜色。默认值为空。


谢谢!你找到了我找不到的问题!我是JavaFX的新手,感谢你的帮助! - Jaboyc

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接