Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SVG not being converted to PNG correctly #919

Open
3 tasks done
akilburge opened this issue May 5, 2021 · 1 comment
Open
3 tasks done

SVG not being converted to PNG correctly #919

akilburge opened this issue May 5, 2021 · 1 comment

Comments

@akilburge
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have verified that I am using the latest version of Magick.NET
  • I have searched open and closed issues to ensure it has not already been reported

Description

I'm seeing a weird results (png is getting clipped) when converting a particular SVG into PNG using Magick.NET (same issue using imagemagick from the console). The PNG is converted properly if a viewBox is defined in the SVG.

Steps to Reproduce

Run the following code:

using System.IO;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using ImageMagick;

namespace ConsoleApp7
{
    class Program
    {
        static async Task Main()
        {
            var httpClient = new HttpClient();

            // #1 - No ViewBox but png converted correctly
            var svg1 = await httpClient.GetStringAsync("https://img.vggcdn.net/svgv2/156660.svg?v=47");
            ConvertSvgToPng(svg1, "./1-svg-no-viewbox-but-converted-correctly.png");

            // #2 - No ViewBox but png converted incorrectly
            var svg2 = await httpClient.GetStringAsync("https://img.vggcdn.net/svgv2/195035.svg?v=14");
            ConvertSvgToPng(svg2, "./2-svg-no-viewbox-but-converted-incorrectly.png");

            // #3 - ViewBox and png converted correctly
            var svg3 = svg2.Replace("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">", "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" viewBox=\"281 11 962 995\">");
            ConvertSvgToPng(svg3, "./3-svg-viewbox-and-converted-correctly.png");
        }

        static void ConvertSvgToPng(string svg, string fileName)
        {
            var settings = new MagickReadSettings { Format = MagickFormat.Svg, BackgroundColor = MagickColors.Transparent };
            using (var image = new MagickImage(Encoding.UTF8.GetBytes(svg), settings))
            {
                image.Write(new FileInfo(fileName), MagickFormat.Png32);
            }
        }
    }
}

System Configuration

  • Magick.NET version: Magick.NET-Q16-AnyCPU 7.23.4
  • Environment (Operating system, version and so on): Windows 10
  • Additional information:
@dlemstra
Copy link
Owner

It turns out that both ImageMagick and the librsvg library are using a viewbox where part of the image is outside the viewbox. This will probably not be easy to fix. I would advise you to add the viewbox when it is missing. Will leave this open in case someone wants to spend time investigating why this is happening.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants