title: From URI to Image in .NET
body: <p>I've a URI like this one <code>data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBzdGFuZGFsb25lPSJubyI/PjwhRE9DVFlQRSBzdmcgUFVCTElDI .....</code> 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.<br/>
  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.<br/>
  How can I render SVG with C# (preferably ASP.Net)?</p>
  
  <p><strong>[EDIT]</strong><br/><br/>
  Solution 1: Use an external .NET library which elaborate svg stream and save it to png file.<br/>
  Solution 2: I moved the problem on the client; with this javascrpit code <br/></p>
  
  <pre><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');                    
              }
  </code></pre>
  
  <p>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:<br/> </p>
  
  <pre><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();
  }
  </code></pre>