I am attempting to convert SVG images to compressed JPG images.
I started by using Aspose.SVG, and implemented a method to do just that.
It is rather slow, but it did manage to make some decent jpg compressions. However, when I tried to get the code through my build pipeline, it failed because it is running on Linux. So I replaced the Aspose.SVG package with the Aspose.SVG.Drawing.SkiaSharp package, which resolves issues in the build pipeline, but introduces a breaking issue when executed locally on my windows machine.
Specifically, it seems that the svg.RenderTo(imageDevice); method is hanging until the thread is destroyed. I tested my rendering options one by one and eventually found that if I removed the “PageSetup” parameter completely, it would work (although the resulting image would be incorrect visually):
using Size = Aspose.Svg.Drawing.Size;
using Aspose.Imaging;
using Aspose.Imaging.ImageOptions;
using Aspose.Svg;
using Aspose.Svg.Drawing;
using Aspose.Svg.Rendering.Image;
using var svg = new SVGDocument(svgStream, options.ImageUrl);
var renderingOptions = new ImageRenderingOptions(ImageFormat.Jpeg)
{
BackgroundColor = options.BackgroundColor,
HorizontalResolution = options.TargetDpi,
VerticalResolution = options.TargetDpi,
UseAntialiasing = options.UseAntialiasing,
//PageSetup = { AnyPage = GetSvgPage(svg, options.TargetDpi, options.SourceDpi) },
};
private static Page GetSvgPage(SVGDocument svg, int targetDpi, int sourceDpi)
{
var viewBox = svg.RootElement.ViewBox.AnimVal;
var widthInPixels = (int)Math.Ceiling(viewBox.Width * targetDpi / sourceDpi);
var heightInPixels = (int)Math.Ceiling(viewBox.Height * targetDpi / sourceDpi);
if (widthInPixels == 0 || heightInPixels == 0)
{
return new Page();
}
var pixelSize = new Size(
Unit.FromPixels(widthInPixels),
Unit.FromPixels(heightInPixels)
);
return new Page(pixelSize, new Margin(0));
}
I have made no other change than the replacement of the package.
The input options for my test case are a targetDpi of 300 and source dpi of 96. Final image is 3508 pixels tall.
I would love to see this being fixed, as it is blocking progress on a project.
I would also like to hear about performance changes that could be made to speed this up, as it currently takes a very long time to render and compress these (admittedly large) images - which might be why it hangs without throwing exceptions.