0

I'am trying to not create a file, and pass xml document straight to a SkiaSharp method Load. I mean, is there the way to imitate a path? So here is the code:

public IActionResult svgToPng(string itemId, string mode = "
{
    var svgSrc = new XmlDocument();
    svgSrc.LoadXml(/*Some xml code*/);

    string svgSaveAs = "save file path";
    var quality = 100;

    var svg = new SkiaSharp.Extended.Svg.SKSvg();
    var pict = svg.Load(svgSrc); // HERE, it needs to be a path, not XmlDocument, but i want to pass straight

    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 = System.IO.File.OpenWrite(svgSaveAs))
    {
        skdata.SaveTo(stream);
    }

    ViewData["Content"] = "PNG file was created out of SVG.";

    return View(); 
}

The Load method seems to be this:

public SKPicture Load(
    using (var stream = File.OpenRead(filename))
    {
        return Load(stream);
    }
}
  • why not store the xml content as string and load that instead – Rahul May 30 '19 at 09:31
  • @rahul it asks for path, i tried to pass a string – narayana raghavendra May 30 '19 at 09:32
  • [Convert your string to a stream](https://stackoverflow.com/questions/1879395/how-do-i-generate-a-stream-from-a-string) and use the [Load overload that takes a stream](https://github.com/mono/SkiaSharp.Extended/blob/master/SkiaSharp.Extended.Svg/source/SkiaSharp.Extended.Svg.Shared/SKSvg.cs#L84) – stuartd May 30 '19 at 09:40
  • Or [use the Load overload that takes an Xmlreader](https://github.com/mono/SkiaSharp.Extended/blob/master/SkiaSharp.Extended.Svg/source/SkiaSharp.Extended.Svg.Shared/SKSvg.cs#L92) – stuartd May 30 '19 at 09:41

1 Answers1

0

look at the code of that library :

https://github.com/mono/SkiaSharp.Extended/blob/master/SkiaSharp.Extended.Svg/source/SkiaSharp.Extended.Svg.Shared/SKSvg.cs

Look at the Load method, it has multiple implementations :

public SKPicture Load(string filename)
{
    using (var stream = File.OpenRead(filename))
    {
        return Load(stream);
    }
}

public SKPicture Load(Stream stream)
{
    using (var reader = XmlReader.Create(stream, xmlReaderSettings, CreateSvgXmlContext()))
    {
        return Load(reader);
    }
}

public SKPicture Load(XmlReader reader)
{
    return Load(XDocument.Load(reader));
}

You will need to pick one of them and use it. Now, nothing stops you from getting the code and adding one extra Load for an XML string for example, but since this is a library you do not control, I'd stick to what you are given.

You could use the XmlReader version, that's probably the closest one to what you want.

CR0N0S.LXIII
  • 349
  • 2
  • 11
Andrei Dragotoniu
  • 6,155
  • 3
  • 18
  • 32