JavaFX图像可以保存得比它大吗?

时间:2015-03-21 13:19:09

标签: canvas printing javafx resize

我决定根据输入的测量值制作一个基本上在画布上为人绘制特定图像的应用程序。然后我想让这个人能够以更大的格式打印出这张图片(或者保存为更大的JPEG,PNG,PDF文件)。

所以在屏幕上它基本上是一个像素的问题,但在打印它将是几英寸长,如果这一切都有意义?

这可能吗?

谢谢!

1 个答案:

答案 0 :(得分:2)

' ave想出了如何缩放图像以便以更大的格式保存它,因此这适用于那些仍在尝试解决的人。

    // Create the canvas
    Stage primaryStage = new Stage();
    Group root = new Group();
    Canvas canvas = new Canvas(500, 600);              
    GraphicsContext gc = canvas.getGraphicsContext2D();       
    drawShapes(gc);
    root.getChildren().add(canvas);
    primaryStage.setScene(new Scene(root));
    primaryStage.show();

    /* If you're going to scale your image by 2, 
    * for example, make sure your image size is large 
    * enough to contain the new image. As you can see, 
    * the image size is double the canvas size. */

    WritableImage wim = new WritableImage(1000, 1200);
    SnapshotParameters spa = new SnapshotParameters();

    // Scale the x and y axis by a factor of 2

    spa.setTransform(Transform.scale(2, 2));
    canvas.snapshot(spa, wim);

    File file = new File("CanvasImage.png");

    try {
        ImageIO.write(SwingFXUtils.fromFXImage(wim, null), "png", file);
    } catch (Exception s) {
    }