We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Poor conversion from SVG to JPEG/PNG

I’m trying to render an SVG as JPEG/PNG and can’t figure out how to get a good quality output.

(I’m using the latest Aspose.CAD .NET nuget package from today, 22.7.0.)

I’m losing all the text, and much of the quality of the diagram. Any thoughts on how to render this properly?

I’m simply using Image.Load(stream) to load the SVG.

And I’ve tried a bunch of options, but currently using this for the JpegOptions:

                    options = new JpegOptions
                    {
                        VectorRasterizationOptions = new CadRasterizationOptions
                        {
                            Quality = new RasterizationQuality { Text = RasterizationQualityValue.High, ObjectsPrecision = RasterizationQualityValue.High, Arc = RasterizationQualityValue.High, Hatch = RasterizationQualityValue.High },
                            PageWidth = width,
                            PageHeight = height,
                            AutomaticLayoutsScaling = true,
                            ExportAllLayoutContent = true,
                            NoScaling = false,
                            ContentAsBitmap = true,
                        }
                    };

Input:
file.svg.zip (138.1 KB)

Output:
file.jpeg (99.1 KB)

Hello, can you try the following and see if this will fix conversion

PngOptions pngOptions = new PngOptions();
pngOptions.setCompressionLevel(0);
Image image = Image.load(sourcePath);
int unitType = image.getUnitType();
int width = image.getWidth();
int height = image.getHeight();
int zoom = 1008000 / width / height;
CadRasterizationOptions cadRasterizationOptions = new CadRasterizationOptions();
cadRasterizationOptions.setPageHeight(width * zoom);
cadRasterizationOptions.setPageWidth(height * zoom);
cadRasterizationOptions.setDrawType(CadDrawTypeMode.UseObjectColor);
cadRasterizationOptions.setUnitType(UnitType.Unitless);

    pngOptions.setVectorRasterizationOptions(cadRasterizationOptions);
    image.save(targetPath, pngOptions);

Please let me know

Appreciate the help. When I try that, I get a really tiny (like 64px square) image.

To be more specific, my source SVG is 6142x4709 and unit type of ‘inch’.

I’m trying to save it as a 2048x1570 JPEG/PNG.

Maybe the zoom calculation needs to be adjusted? With the calculation there, the zoom percentage is really tiny - 0.348.

I’ve tried different variations of those options, and still not getting any text on the image and missing other objects, with the SVG I uploaded.

@kirkmarple,
Hello. I have created CADNET-8724 to investigate this issue. Right now I think that the text disappears because of some reason, and I’m not sure that this relates to size - we need to check. I guess you should not use ContentAsBitmap, it is useful to embed drawing as raster into vector format - just not your case, but in common may decrease the quality. I’m afraid your task is complex to solve, because you are trying to export big vector image into small (relatively) raster, so it it expected that the quality will not be so good. Let us research what’s going on with text and come back.

1 Like