【问题标题】:JavaFX 8 Canvas Snapshot with alpha带有 alpha 的 JavaFX 8 画布快照
【发布时间】:2015-11-05 04:56:17
【问题描述】:

我目前正在开发一个绘图程序(类似于 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 范围内给出解决方案。

【问题讨论】:

    标签: java canvas javafx


    【解决方案1】:

    在拍摄快照之前,将您的 SnapshotParameters 的Set the fill 设置为Color.TRANSPARENT

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

    来自 javadoc:

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

    【讨论】:

    • 谢谢!你找到了我找不到的问题!我是 JavaFX 的新手,非常感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2014-11-30
    • 1970-01-01
    • 2016-04-03
    • 1970-01-01
    • 2011-10-27
    • 2011-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多