将SVG base64字符串转换为图像-C#

时间:2018-11-01 11:10:31

标签: c# visual-studio svg jsignature

我将svg字符串发布到服务器,以便

  1. 将svg字符串转换为图片(png)
  2. 将图像保存到数据库(二进制数据)

但是我只能将字符串成功转换为图像。

SVG字符串是从https://willowsystems.github.io/jSignature/#/demo/生成的,在这里我使用svgbase64提取。

示例内容:"image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+PCFET0NUWVBFIHN2ZyBQVUJMSUMgIi0vL1czQy8vRFREIFNWRyAxLjEvL0VOIiAiaHR0cDovL3d3dy53My5vcmcvR3JhcGhpY3MvU1ZHLzEuMS9EVEQvc3ZnMTEuZHRkIj48c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmVyc2lvbj0iMS4xIiB3aWR0aD0iNDAzIiBoZWlnaHQ9IjEwNiI+PHBhdGggZmlsbD0ibm9uZSIgc3Ryb2tlPSIjMDAwMDAwIiBzdHJva2Utd2lkdGg9IjIiIHN0cm9rZS1saW5lY2FwPSJyb3VuZCIgc3Ryb2tlLWxpbmVqb2luPSJyb3VuZCIgZD0iTSAxMjUgMSBjIC0wLjEyIDAuNjggLTMuOSAyNi4xOSAtNyAzOSBjIC0xLjkgNy44NyAtNC44NiAxNS44MiAtOCAyMyBjIC0xLjQgMy4xOSAtMy42MyA2Ljc4IC02IDkgYyAtMi41NiAyLjQgLTYuNTggNC45IC0xMCA2IGMgLTUuMzUgMS43MiAtMTEuOTYgMS42MiAtMTggMyBjIC0xMC4yNyAyLjM1IC0xOS43MSA1LjcxIC0zMCA4IGMgLTguMDYgMS43OSAtMTYuMyAzLjI4IC0yNCA0IGMgLTIuNTUgMC4yNCAtNi42MSAwLjM5IC04IC0xIGMgLTIuOTMgLTIuOTMgLTYuNzMgLTkuOTEgLTggLTE1IGMgLTIuNDkgLTkuOTYgLTMuMDggLTIxLjcyIC00IC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LTQuNTggMC44NyAtOS45OCAwIC0xNCBjIC0wLjY0IC0yLjk1IC0yLjk4IC02LjM1IC01IC05IGMgLTMuMTkgLTQuMTkgLTcuMjQgLTkuMzcgLTExIC0xMiBjIC0yLjEzIC0xLjQ5IC02LjMgLTEuMSAtOSAtMiBjIC0xLjA2IC0wLjM1IC0xLjk1IC0xLjc1IC0zIC0yIGMgLTQuMTMgLTAuOTcgLTkuMzIgLTEuOTEgLTE0IC0yIGMgLTExLjkyIC0wLjI0IC0yNC4xMiAtMC4wNiAtMzYgMSBjIC02LjczIDAuNiAtMTQuMiAxLjUgLTIwIDQgYyAtOC4wNSAzLjQ4IC0xNS44IDEwLjI4IC0yNCAxNSBjIC0yLjg1IDEuNjQgLTYuMzIgMi4yNSAtOSA0IGMgLTYuNzkgNC40NSAtMTMuMDcgMTAuMTUgLTIwIDE1IGwgLTEwIDYiLz48L3N2Zz4=";
    var bytes = Convert.FromBase64String(svgContent);


    using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(svgContent)))
    {
        using (Bitmap bm2 = new Bitmap(ms))
        {
            bm2.Save("c:\\temp" + "myImage.jpg");
        }
    }

将流设置为位图时出现异常。我想问题出在我的svgContent上,但不确定是什么。

enter image description here

1 个答案:

答案 0 :(得分:0)

将xamarin nuget的SkiaSharp.Svg与Microsoft的SkiaSharp nuget一起使用,这是跨平台BTW:

var svgContent = "PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+PCFET0NUWVBFIHN2ZyBQVUJMSUMgIi0vL1czQy8vRFREIFNWRyAxLjEvL0VOIiAiaHR0cDovL3d3dy53My5vcmcvR3JhcGhpY3MvU1ZHLzEuMS9EVEQvc3ZnMTEuZHRkIj48c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmVyc2lvbj0iMS4xIiB3aWR0aD0iNDAzIiBoZWlnaHQ9IjEwNiI+PHBhdGggZm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gMi43MSA4LjQ3IDcuNjYgMTMgMTAgYyA0LjYzIDIuMzkgMTAuNTQgMy43MiAxNiA1IGMgNS45NCAxLjQgMTEuOTYgMi42OCAxOCAzIGMgMTIuOTEgMC42OCAyNS43IDAuNzMgMzkgMCBjIDM2LjI2IC0yIDczLjAzIC0zLjk1IDEwNiAtOCBjIDUuNDIgLTAuNjcgMTAuNSAtNS4xMiAxNiAtNyBjIDYuMTggLTIuMTIgMTMuMyAtMy4wMiAxOSAtNSBjIDEuNDUgLTAuNSAyLjc2IC0xLjg4IDQgLTMgYyAyLjQ1IC0yLjIzIDUuNjggLTQuNTEgNyAtNyBjIDEuMzYgLTIuNTYgMS43MiAtNi42NyAyIC0xMCBjIDAuMzggLTQuNTggMC44NyAtOS45OCAwIC0xNCBjIC0wLjY0IC0yLjk1IC0yLjk4IC02LjM1IC01IC05IGMgLTMuMTkgLTQuMTkgLTcuMjQgLTkuMzcgLTExIC0xMiBjIC0yLjEzIC0xLjQ5IC02LjMgLTEuMSAtOSAtMiBjIC0xLjA2IC0wLjM1IC0xLjk1IC0xLjc1IC0zIC0yIGMgLTQuMTMgLTAuOTcgLTkuMzIgLTEuOTEgLTE0IC0yIGMgLTExLjkyIC0wLjI0IC0yNC4xMiAtMC4wNiAtMzYgMSBjIC02LjczIDAuNiAtMTQuMiAxLjUgLTIwIDQgYyAtOC4wNSAzLjQ4IC0xNS44IDEwLjI4IC0yNCAxNSBjIC0yLjg1IDEuNjQgLTYuMzIgMi4yNSAtOSA0IGMgLTYuNzkgNC40NSAtMTMuMDcgMTAuMTUgLTIwIDE1IGwgLTEwIDYiLz48L3N2Zz4=";           
   var byteArray = Convert.FromBase64String(svgContent);
   using (var stream = new MemoryStream(byteArray))
   {
     var bitmap = new SKBitmap(500, 500);
     var canvas = new SKCanvas(bitmap);
     // load the SVG
     var svg = new SkiaSharp.Extended.Svg.SKSvg(new SKSize(100, 100));
     svg.Load(stream);
     // draw the SVG to the bitmap
     canvas.DrawPicture(svg.Picture);
     var skData = SKImage.FromBitmap(bitmap).Encode(SKEncodedImageFormat.Png, 100);
     using (var file = new FileStream(@"C:\temp\0\1.png", FileMode.Create))
     {
        skData.SaveTo(file);
     }
}

或通过vvv使用Svg nuget

var svgContent = "PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+PCFET0NUWVBFIHN2ZyBQVUJMSUMgIi0vL1czQy8vRFREIFNWRyAxLjEvL0VOIiAiaHR0cDovL3d3dy53My5vcmcvR3JhcGhpY3MvU1ZHLzEuMS9EVEQvc3ZnMTEuZHRkIj48c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmVyc2lvbj0iMS4xIiB3aWR0aD0iNDAzIiBoZWlnaHQ9IjEwNiI+PHBhdGggZmlsbD0ibm9uZSIgc3Ryb2tlPSIjMDAwMDAwIiBzdHJva2Utd2lkdGg9IjIiIHN0cm9rZS1saW5lY2FwPSJyb3VuZCIgc3Ryb2tlLWxpbmVqb2luPSJyb3VuZCIgZD0iTSAxMjUgMSBjIC0wLjEyIDAuNjggLTMuOSAyNi4xOSAtNyAzOSBjIC0xLjkgNy44NyAtNC44NiAxNS44MiAtOCAyMyBjIC0xLjQgMy4xOSAtMy42MyA2Ljc4IC02IDkgYyAtMi41NiAyLjQgLTYuNTggNC45IC0xMCA2IGMgLTUuMzUgMS43MiAtMTEuOTYgMS42MiAtMTggMyBjIC0xMC4yNyAyLjM1IC0xOS43MSA1LjcxIC0zMCA4IGMgLTguMDYgMS43OSAtMTYuMyAzLjI4IC0yNCA0IGMgLTIuNTUgMC4yNCAtNi42MSAwLjM5IC04IC0xIGMgLTIuOTMgLTIuOTMgLTYuNzMgLTkuOTEgLTggLTE1IGMgLTIuNDkgLTkuOTYgLTMuMDggLTIxLjcyIC00IC0zMyBjIC0wLjc4IC05LjUxIC0xLjI2IC0xOS4zMSAtMSAtMjggYyAwLjA2IC0xLjk2IDE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gLTkuMzIgLTEuOTEgLTE0IC0yIGMgLTExLjkyIC0wLjI0IC0yNC4xMiAtMC4wNiAtMzYgMSBjIC02LjczIDAuNiAtMTQuMiAxLjUgLTIwIDQgYyAtOC4wNSAzLjQ4IC0xNS44IDEwLjI4IC0yNCAxNSBjIC0yLjg1IDEuNjQgLTYuMzIgMi4yNSAtOSA0IGMgLTYuNzkgNC40NSAtMTMuMDcgMTAuMTUgLTIwIDE1IGwgLTEwIDYiLz48L3N2Zz4=";           
var byteArray = Convert.FromBase64String(svgContent);
using (var stream = new MemoryStream(byteArray))
{
   var svgDocument = SvgDocument.Open<SvgDocument>(stream);
   var bitmap = svgDocument.Draw();
   bitmap.Save(@"C:\temp\0\1.png");
}