How to produce a System.Drawing.Image from SVG on .net core?

Viewed 8189

I have found many libraries to read SVG and transform it to System.Drawing.Image or png in C# framework, but I cannot find any way to do it in .net core.

And if I use Image.FromFile, I get an OutOfMemoryException (supposedly because SVG is not a rasterized format).

Any tips on how to use Image to read SVG or any open source library that works in .net core?

2 Answers
  1. Skiasharp by Xamarin team seems to be a good choice. There's already a document of API on docs.microsoft.com. For more detailed information, see Mono/SkiaSharp and Mono/mono/SkiaSharp.Extended

  2. You can install the offical svg extension on nuget by dotnet add package SkiaSharp.Svg:

    <PackageReference Include="SkiaSharp.Svg" Version="1.60.0" />
    
  3. Demo:

        var svgSrc=Path.Combine(Directory.GetCurrentDirectory(),"img.svg");
        string svgSaveAs = "xyz.png";
        var quality = 100;
    
        var svg = new SkiaSharp.Extended.Svg.SKSvg();
        var pict = svg.Load(svgSrc);
        var dimen = new SkiaSharp.SKSizeI(
            (int) Math.Ceiling(pict.CullRect.Width),
            (int) Math.Ceiling(pict.CullRect.Height)
        );
        var matrix = SKMatrix.MakeScale(1,1);
        var img = SKImage.FromPicture(pict,dimen,matrix);
    
        // convert to PNG
        var skdata = img.Encode(SkiaSharp.SKEncodedImageFormat.Png,quality);
        using(var stream = File.OpenWrite(svgSaveAs)){
            skdata.SaveTo(stream);
        }
    

Screenshot:

enter image description here

You can use ImageMagick to convert svg to any format.

<PackageReference Include="Magick.NET-Q16-AnyCPU" Version="7.14.0" />

Below method converts svg base64 string to other formats.

public static string Base64ToImageStream(string base64String)
    {
        byte[] imageBytes = Convert.FromBase64String(base64String);
        using (MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
        {
            using (var msOut = new MemoryStream())
            {
                MagickReadSettings readSettings = new MagickReadSettings()
                {
                    Format = MagickFormat.Svg,
                    Width = 60,
                    Height = 40,
                    BackgroundColor = MagickColors.Transparent
                };

                using (MagickImage image = new MagickImage(imageBytes, readSettings))
                {
                    image.Format = MagickFormat.Png; // Specify the format you need
                    image.Write(msOut);
                    byte[] data = image.ToByteArray();
                    return Convert.ToBase64String(data);

                    // In case if you want the output in stream
                    // byte[] imgByte = Convert.FromBase64String(pngBase64);
                    // var pngStream = new MemoryStream(imgByte, 0, imgByte.Length);
                    // return pngStream;
                }
            }
        }            
    }
Related