Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 840
torendil Avatar asked Oct 29 '25 09:10

torendil


2 Answers

  1. Skiasharp by Xamarin team seems to be a good choice. There's already a document of API on learn.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

like image 158
itminus Avatar answered Oct 31 '25 05:10

itminus


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;
                }
            }
        }            
    }
like image 22
Pramod KP Avatar answered Oct 31 '25 05:10

Pramod KP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!