Convert SVG to JPG in Java

In this article, you learn how to convert an SVG to JPG using Aspose.HTML for Java and how to apply ImageSaveOptions and MemoryStreamProvider. You can easily use Java examples to convert SVG to JPG, detailed here for converting SVG to PNG, BMP, GIF, and TIFF images. Just set the required ImageFormat to ImageSaveOptions!

Converting SVG to any supported image format follows the same mandatory steps:

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, TIFF, and GIF. The default ImageFormat is PNG.

Convert SVG to JPG by a single line of code

Converting SVG to JPG gives you a raster image that can be easily shared, viewed, or emailed. 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 JPG in your Java application literally with a single line of code!

1  // Invoke the convertSVG() method for SVG to JPG conversion
2  com.aspose.html.converters.Converter.convertSVG(Path.combine(getDataDir(), "shapes.svg"), new ImageSaveOptions(ImageFormat.Jpeg), Path.combine(getOutputDir(), "convert-with-single-line.jpg"));

Convert SVG to JPG

SVG files are great for website graphics, but not all web browsers support them. Converting SVG to JPG ensures that the image will load properly on any web browser, allowing for a better user experience. Moreover, JPG images are easy to share, send by email, embed in reports or presentations, etc. In the following Java example, we will walk through the step-by-step instructions for converting SVG to JPG with default save options:

  1. 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.
  2. Use ImageSaveOptions() constructor to create a new ImageSaveOptions object.
  3. Call the convertSVG() method of the Converter class to save SVG as a JPG image. In the example we use the convertSVG(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 JPG 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 ='80' fill='none' stroke='green' stroke-width='5' />",
 4            "</svg>");
 5
 6  // Prepare a path for converted file saving 
 7  String savePath = Path.combine(getOutputDir(), "green-circle.jpg");
 8
 9  // Initialize an ImageSaveOptions instance
10  ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
11
12  // Convert SVG to JPG
13  com.aspose.html.converters.Converter.convertSVG(code, ".", options, savePath);

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.

You can download the complete examples and data files from GitHub.

To learn more about ImageSaveOptions please read Fine-Tuning Converters article.

Convert SVG to JPG using ImageSaveOptions

If your scenario is required rendering SVG document, for instance, to JPG file format with custom save options, the following example demonstrates how that is simple:

  1. Load an SVG file using SVGDocument class.
  2. 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 JPG image:
    • Use the setHorizontalResolution() and setVerticalResolution() 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.
  3. Use the convertSVG(document, options, savePath) method of Converter class to save SVG as a JPG image. You need to pass the SVGDocument, ImageSaveOptions, and output file path to the convertSVG() method to perform the conversion.
 1  // Prepare a path to a source SVG file
 2  String documentPath = Path.combine(getDataDir(), "flower.svg");
 3
 4  // Prepare a path for converted file saving 
 5  String savePath = Path.combine(getOutputDir(), "flower-options.jpg");
 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 resolutions, SmoothingMode and change the background color to AliceBlue 
13  ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
14  options.setSmoothingMode(SmoothingMode.HighQuality);
15  options.setHorizontalResolution(new Resolution(200, UnitType.AUTO));
16  options.setVerticalResolution(new Resolution(200, UnitType.AUTO));
17  com.aspose.html.drawing.Color.getAliceBlue().CloneTo(options.getBackgroundColor());
18
19  // Convert SVG to JPG
20  com.aspose.html.converters.Converter.convertSVG(document, options, savePath);

Check the quality of SVG to JPG conversion with our online SVG to JPG Converter Upload, convert your files and get the result in a few seconds. Try our forceful SVG to JPG Converter for free now!

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.