Home
Categories
Dictionary
Download
Project Details
FAQ
License

Producing a JavaFX image



The SVGImage.toImage() method allows to create a JavaFX image from the initial image. It is possible to specify the width of the result by using one of the following methods:
If you use the ScaleQuality.RENDER_QUALITY value for the quality parameter, the result will be scaled at the SVG level before being converted to a JavaFX image.

Setting the snapshot parameters

The SVGImage.toImage(SVGSnapshotParameters) method allow to specify the parameters to use to produce the snapshot.

The SVGSnapshotParameters encapsulate the SnapshotParameters to use to produce the image.

Default snapshot parameters

By default the SnapshotParameters used to generate the images will use a default SnapshotParameters It is possible to set the parameters to use by using the following methods: It is possible to specify the default SVGSnapshotParameters to use when creating snapshots by using the SVGImage.setDefaultSnapshotParameters(SVGSnapshotParameters) method.

Defining the viewport of the resulting image

The SVGSnapshotParameters.setViewportType(short type) allows to specify which viewport to choose for the resulting image: For example, for the following SVG content:
   <svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 -960 960 960" width="24px" fill="#1f1f1f">
      <path d="M160-440v-80h640v80H160Z"/>
    </svg>
If you use the following code:
   URL url = <my SVG content>
   SVGImage result = SVGLoader.load(url);
   Image img = result.toImage();      
The width and height of the image will be (16 px, 2 px), which is the size of the path.

If you use the following code:
   URL url = <my SVG content>
   SVGImage result = SVGLoader.load(url);
   SVGSnapshotParameters params = new SVGSnapshotParameters();
   params.setViewportType(SVGSnapshotParameters.USE_BEST_VIEWPORT_SIZE);
   Image img = result.toImage(params);   
The width and height of the image will be (24 px, 24 px), which is the size of the viewport.

Examples

   SVGImage img = SVGLoader.load(<my SVG file>);
      
   // the resulting image will have a width of 100 pixels
   Image fxImg = img.toImage(100); 
   // the resulting image will be scaled by a 0.5 factor    
   Image fxImg2 = img.toImageScaled(0.5d); 
   // the resulting image will have a width of 50 pixels, but the image will be scaled at the SVG level before creating the image
   Image fxImg2 = img.toImage(ScaleQuality.RENDER_QUALITY, 50); 

See also


Categories: Fromsvg

Copyright 2021-2022 Herve Girod. All Rights Reserved. Documentation and source under the BSD-3-Clause License