2015-11-04 75 views
2

我目前正在绘制程序(类似于GIMP和Photoshop),为了做到这一点,我将需要图层。我创建了一个名为JImage的类,它有一个ArrayList<Canvas> layers和一些方法。JavaFX 8 Canvas快照与阿尔法

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图层的多个画布中获取图像?我宁愿为这个解决方案使用JavaFX,所以请在JavaFX的范围内给出解决方案。

回答

4

Set the fill您的快照参数为Color.TRANSPARENT,然后拍摄快照。

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

根据JavaDoc:

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

+0

谢谢!你发现了我找不到的问题!我是JavaFX的新手,非常感谢您的帮助! – Jaboyc