2015-11-04 55 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