Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Aspose.HTML for Java converts SVG code, files, URLs, and SVGDocument objects to JPG. Pass the source,
ImageSaveOptions with ImageFormat.Jpeg, and output path to a suitable
Converter.convertSVG() overload.
SVG is resolution-independent, while JPG is a widely supported raster format with lossy compression. SVG to JPG conversion is useful for thumbnails, previews, presentations, and systems that require a conventional bitmap image. Because JPG does not support transparency, choose an output background color when transparent SVG areas must have a predictable appearance.
The
Converter.convertSVG() overload used below accepts SVG markup, a base URI, image save options, and an output path. The base URI resolves relative resources referenced by the SVG; "." refers to the current directory.
The example converts an inline SVG circle to output.jpg with ImageFormat.Jpeg.
1// Convert SVG to JPG in a few lines using Java
2
3// Prepare SVG code
4String code = "<svg xmlns='http://www.w3.org/2000/svg'>\n" +
5 "<circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red' />\n" +
6 "</svg>\n";
7
8// Invoke the convertSVG() method to convert SVG to image
9Converter.convertSVG(code, ".", new ImageSaveOptions(ImageFormat.Jpeg), "output.jpg");Follow these steps to convert an SVG document to JPG:
ImageSaveOptions and select ImageFormat.Jpeg.Converter.convertSVG() with the document, save options, and output path.The following example writes SVG markup to document.svg, loads it as an SVGDocument, and converts it to output.jpg.
1// Convert SVG to JPG using Java
2
3// Prepare SVG code and save it to a file
4String code = "<svg xmlns='http://www.w3.org/2000/svg'>\n" +
5 "<circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red' />\n" +
6 "</svg>\n";
7try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.svg")) {
8 fileWriter.write(code);
9}
10
11// Initialize an SVG document from the SVG file
12SVGDocument document = new SVGDocument("document.svg");
13
14// Initialize ImageSaveOptions
15ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
16
17// Convert SVG to JPG
18Converter.convertSVG(document, options, "output.jpg");The
ImageSaveOptions class controls raster output. The same class supports JPG, PNG, BMP, GIF, and TIFF by changing the ImageFormat value.
| Method | Purpose |
|---|---|
setFormat(value) | Selects the output image format. |
setPageSetup(value) | Sets the output page size and margins. |
setBackgroundColor(value) | Sets the color behind the converted SVG content. |
setHorizontalResolution(value) | Sets horizontal output resolution in pixels per inch. |
setVerticalResolution(value) | Sets vertical output resolution in pixels per inch. |
setSmoothingMode(value) | Sets the smoothing quality used during rasterization. |
To convert SVG to JPG with custom settings:
ImageSaveOptions for JPEG and configure the required page and rendering settings.Converter.convertSVG() with the source, configured options, and output path.The next example converts flower.svg to flower.jpg. It uses a 200 × 200 pixel page, an Alice Blue background, and high-quality smoothing.
1// Convert SVG to JPG in Java with custom settings
2
3// Prepare SVG code and save it to a file
4String code =
5 "<svg width='450' height='450' xmlns='http://www.w3.org/2000/svg'>" +
6 " <g fill='RoyalBlue'>" +
7 " <rect x='100' y='100' rx='25' ry='25' width='200' height='56' />" +
8 " <rect x='100' y='100' rx='25' ry='25' width='200' height='56' transform ='rotate(90 200 128)' />" +
9 " <rect x='100' y='100' rx='25' ry='25' width='200' height='56' transform ='rotate(-45 200 128)' />" +
10 " <rect x='100' y='100' rx='25' ry='25' width='200' height='56' transform ='rotate(45 200 128)' />" +
11 " </g>" +
12 " <circle cx='200' cy='128' r='28' stroke='pink' stroke-width='50' stroke-dasharray='3 13' fill='Orange' />" +
13 " <circle cx='200' cy='128' r='5' />" +
14 "</svg>";
15
16try (java.io.FileWriter fileWriter = new java.io.FileWriter("flower.svg")) {
17 fileWriter.write(code);
18}
19
20// Initialize ImageSaveOptions and set up smoothing mode, page size, and background color
21ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
22PageSetup pageSetup = new PageSetup();
23options.setSmoothingMode(SmoothingMode.HighQuality);
24Page anyPage = new Page();
25anyPage.setSize(new Size(Length.fromPixels(200), Length.fromPixels(200)));
26pageSetup.setAnyPage(anyPage);
27options.setPageSetup(pageSetup);
28options.setBackgroundColor(Color.getAliceBlue());
29
30// Call the convertSVG() method to convert the "flower.svg" file to a JPEG image
31Converter.convertSVG("flower.svg", options, "flower.jpg");Download complete Java examples and data files from GitHub. See Fine-Tuning Converters for additional conversion settings.
You can use the free online SVG to JPG Converter for occasional manual conversions. Aspose.HTML for Java is a commercial API; you can evaluate it before licensing it for production use.
JPG has no alpha channel, so it cannot preserve transparency. Set a background color with ImageSaveOptions.setBackgroundColor() when transparent regions must be filled with a specific color.
Use the free online SVG to JPG Converter for quick manual conversion without installing software. Use Aspose.HTML for Java when SVG to JPG conversion must run programmatically.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.