I've a URI like this one data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBzdGFuZGFsb25lPSJubyI/PjwhRE9DVFlQRSBzdmcgUFVCTElDI .....
on my web client and what I've to do, is to send that uri to an ASP.NET server and, always on server side, generate the corresponding png image.
Everything works fine if i set the uri to an img.src element of the page, but server side I can't find a way to save this stream inside a file.
How can I render SVG with C# (preferably ASP.Net)?
[EDIT]
Solution 1: Use an external .NET library which elaborate svg stream and save it to png file.
Solution 2: I moved the problem on the client; with this javascrpit code
function (svg_uri) {
var image = new Image();
image.src = svg_uri;
image.onload = function () {
var canvas = document.createElement('canvas');
canvas.width = image.width;
canvas.height = image.height;
var context = canvas.getContext('2d');
context.drawImage(image, 0, 0);
return canvas.toDataURL('image/png');
}
I can get the new URI with the png content; so when I receive it on the server what I need to do is save it with the following code:
var bytes = Convert.FromBase64String(base64string.Replace("data:image/png;base64,",""));
using (var img = new FileStream(filePath, FileMode.Create))
{
img.Write(bytes ,0, bytes.Length);
img.Flush();
}
Just remove the beginning portion of the string "data:image/svg+xml;base64," everything after the comma is the actual base64 image, and you can save that image in .NET like this:
To help further clarify:
and the resulting result.svg:
If you want a png you can use Inkscape.
Here is a link to a tutorial: http://www.31a2ba2a-b718-11dc-8314-0800200c9a66.com/2012/09/how-to-convert-svg-data-to-png-image.html