Convert SVG to PNG in Java
Each image format has unique strengths and weaknesses. SVG is great for creating graphics that can be scaled up or down without losing quality. But sometimes you need to convert SVG files to other image formats like PNG, JPG, BMP, TIFF, or GIF. Overall, SVG conversions can be helpful for developers and designers who must work with different image formats for various purposes. In addition, knowing how to convert between formats can help ensure your graphics and images are accessible and usable for a wider audience.
Aspose.HTML for Java library provides a wide range of SVG conversions to images, such as JPG, PNG, BMP, TIFF, and GIF. Converting SVG to any supported image format follows the same mandatory steps:
- Opening an SVG file.
- Creating an ImageSaveOptions object and specifying image format.
- Converting SVG to chosen image format.
The only difference is in specifying the output image format using the
ImageSaveOptions class. The ImageSaveOptions(format)
constructor initializes the options object with the format specifying. You can set the image format to JPG, PNG, BMP, GIF, or TIFF. The default ImageFormat
is PNG.
In this article, you learn how to convert an SVG to PNG using Aspose.HTML for Java and how to apply
ImageSaveOptions and
MemoryStreamProvider. You can easily use Java examples to convert SVG to PNG, detailed here for converting
SVG to JPG, BMP, GIF, and TIFF images. Just set the required ImageFormat
to ImageSaveOptions!
Convert SVG to PNG by a single line of code
By converting SVG to PNG, you get a raster image that can be easily shared, viewed, sent by email, and compressed without losing of quality. The static methods of the Converter class are primarily used as the easiest way to convert an SVG code into various formats. You can convert SVG to PNG in your Java application literally with a single line of code!
1 // Invoke the convertSVG() method for SVG to PNG conversion
2 com.aspose.html.converters.Converter.convertSVG(Path.combine(getDataDir(), "shapes.svg"), new ImageSaveOptions(), Path.combine(getOutputDir(), "convert-with-single-line.png"));
Convert SVG to PNG
In the following Java example, we will walk through the step-by-step instructions for converting SVG to PNG with default save options:
- Load an SVG file. You can load SVG from a file, SVG code, or URL. In the following example, we prepare SVG code to create SVG from scratch and pass it directly to
convertSVG()
method. - Use ImageSaveOptions() constructor to create a new ImageSaveOptions object. By default, the Format property is PNG.
- Call the
convertSVG()
method of the Converter class to save SVG as a PNG image. In the example we use theconvertSVG(content, baseUri, options, outputPath)
method of the Converter class that takes four parameters: string with SVG code to be converted, the base folder for the input SVG file, an instance of ImageSaveOptions class and the output file path where the converted image will be saved.
The following Java code snippet shows how to convert SVG to PNG using Aspose.HTML for Java.
1 // Prepare SVG code
2 String code = StringExtensions.concat("<svg xmlns='http://www.w3.org/2000/svg'>",
3 "<circle cx ='100' cy ='100' r ='60' fill='none' stroke='red' stroke-width='10' />",
4 "</svg>");
5
6 // Prepare a path for converted file saving
7 String savePath = Path.combine(getOutputDir(), "circle.png");
8
9 // Initialize an ImageSaveOptions instance
10 ImageSaveOptions options = new ImageSaveOptions();
11
12 // Convert SVG to PNG
13 com.aspose.html.converters.Converter.convertSVG(code, ".", options, savePath);
You can download the complete examples and data files from GitHub.
Save Options
The ImageSaveOptions class provides methods that give you full control over a wide range of parameters and improve the process of converting SVG to image file formats. You can specify the image format, page size, margins, compression level, CSS media-type, etc.
Method | Description |
---|---|
setCompression(value) | Sets the Tagged Image File Format (TIFF) Compression. By default this property is Compression.LZW . |
getCss | Gets a CssOptions object which is used for configuration of CSS properties processing. |
setFormat(value) | Sets ImageFormat (JPG, PNG, BMP, TIFF, or GIF). By default this property is ImageFormat.Png . |
setBackgroundColor(value) | Sets Color which will fill background of every page. Default value is Color.Transparent(Color.getTransparent()) . |
setPageSetup(value) | Gets a page setup object is used for configuration output page-set. |
setHorizontalResolution(value) | Sets horizontal resolution for output images in pixels per inch. The default value is 300 dpi. |
setVerticalResolution(value) | Sets vertical resolution for output images in pixels per inch. The default value is 300 dpi. |
setSmoothingMode(value) | Sets the rendering quality for this image. |
getText() | Gets a TextOptions object which is used for configuration of text rendering. |
Convert SVG to PNG using ImageSaveOptions
If your scenario is required rendering SVG document, for instance, to PNG file format with custom save options, the following example demonstrates how that is simple:
- Load an SVG file using SVGDocument class.
- Create a new
ImageSaveOptions object and specify the required save options. In the following example, we apply a custom resolutions and background color for the resulting PNG image:
- Use the
setHorizontalResolution()
andsetVerticalResolution()
methods to set the horizontal and vertical resolution of the image to 200. - Use the
setBackgroundColor()
method to set the background color for every page. - Use the
setSmoothingMode()
method to set the quality of the image smoothing to high.
- Use the
- Use the
convertSVG(document, options, savePath)
method of Converter class to save SVG as a PNG image. You need to pass the SVGDocument, ImageSaveOptions, and output file path to theconvertSVG()
method to perform the conversion.
1 // Prepare a path to a source SVG file
2 String documentPath = Path.combine(getDataDir(), "flower1.svg");
3
4 // Prepare a path for converted file saving
5 String savePath = Path.combine(getOutputDir(), "flower-options.png");
6
7 // Initialize an SVG document from the file
8 SVGDocument document = new SVGDocument(documentPath);
9 try { }
10 finally { if (document != null) document.dispose(); }
11
12 // Initialize ImageSaveOptions. Set up the SmoothingMode, resolutions, and change the background color to AliceBlue
13 ImageSaveOptions options = new ImageSaveOptions();
14 options.setHorizontalResolution(new Resolution(200, UnitType.AUTO));
15 options.setVerticalResolution(new Resolution(200, UnitType.AUTO));;
16 com.aspose.html.drawing.Color.getAliceBlue().CloneTo(options.getBackgroundColor());
17 options.setSmoothingMode(SmoothingMode.HighQuality);
18
19 // Convert SVG to PNG
20 com.aspose.html.converters.Converter.convertSVG(document, options, savePath);
Aspose.HTML offers a free online SVG to PNG Converter that converts SVG to PNG with high quality, easy and fast. This tool helps you create high-quality images and graphics that are compatible with a wide range of platforms and devices. So why not try? Just upload, convert your files and get the result in a few seconds!