title: Convert from a DataUrl to an Image in C# and write a file with the bytes
body: <p>Hello I have signature like this:</p>
  
  <p><img src="https://i.stack.imgur.com/6FjbW.png" alt="enter image description here"></p>
  
  <p>which is encoded to a DataUrl specifically this string:</p>
  
  <p><em>"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAAAADICAYAAADGFbfiAAAYlElEQVR4Xu2dC8w1R1nHQSCIgIKVGLmoiLciFwUs... (long string)"</em></p>
  
  <p>What i want to do is Convert this DataUrl to an PNG Image, and save the image to the device, this is what i am doing so far:</p>
  
  <pre><code>if (newItem.FieldType == FormFieldType.Signature)
  {
       if (newItem.ItemValue != null)
       {
             //string completeImageName = Auth.host + "/" + li[i];
             string path;
             string filename;
             string stringName = newItem.ItemValue;
  
             var base64Data = Regex.Match(stringName, @"data:image/(?&lt;type&gt;.+?),(?&lt;data&gt;.+)").Groups["data"].Value;
             var binData = Convert.FromBase64String(base64Data);
  
             path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  
             filename = Path.Combine(path, base64Data);
  
             if (!File.Exists(filename))
             {
                   using (var stream = new MemoryStream(binData))
                   {
  //Code crashing here--------------------------
                        File.WriteAllBytes(filename, binData);
                    }
              }
  
          newItem.ItemValue = filename;
  
      }
  }
  
           App.Database.SaveReportItem(newItem);
  </code></pre>
  
  <p>But my code is making my application to crash specifically in this line:</p>
  
  <p>File.WriteAllBytes(filename, binData);</p>
  
  <p>The sample I am using as reference (<a href="https://gist.github.com/vbfox/484643" rel="noreferrer">Link</a>) is using a PictureBox but with Xamarin there is no use of a pictureBox.</p>
  
  <p>Any Ideas?</p>