title: Save image using JQuery and WebMethod
body: <p>I am trying to save image to a folder using JQuery and WebMethod in vb.net. But I got <strong>parameter is not valid</strong> exception while I am  trying to convert the stream to image.</p>
  
  <p>HTML</p>
  
  <pre><code>&lt;div&gt;
          &lt;input id="imgInput" type="file"  name="file"/&gt;
          &lt;img id="imagePreview" src="" alt="Item Image" width="96" height="80"/&gt;
          &lt;input id="Button1" type="button" value="save image"  onclick="saveimage();"/&gt;
      &lt;/div&gt;
      &lt;script type="text/javascript"&gt;
          $(document).ready(function () {
              $("#imgInput").change(function () {
  
                  readURL(this);
  
              });
  
          });
          function readURL(input) {
              if (input.files &amp;&amp; input.files[0]) {
                  var reader = new FileReader();
                  reader.onload = function (e) {
  
                      $('#imagePreview').attr('src', e.target.result);
                  }
                  reader.readAsDataURL(input.files[0]);
              }
          }
  
          function saveimage() {
              var imageInByte = encodeURIComponent($('#imagePreview').attr('src'));
  
              var fname = $("#imgInput").val();
              var splitval = fname.split('.');
              var fnames = Math.floor((Math.random() * 10000000) + 1);
              var submitval = JSON.stringify({ data: imageInByte, fname: fnames, ext: splitval[1] });
            //  alert(submitval);
              $.ajax({
                  type: "POST",
                  contentType: "application/json;charset=utf-8",
                  datatype: "json",
                  data: submitval,
                  url: "test.aspx/saveimage",
                  success: function (data) {
                  }
              });
          }
      &lt;/script&gt;
  </code></pre>
  
  <p>web service</p>
  
  <pre><code>&lt;WebMethod()&gt;
      Public Shared Sub saveimage(ByVal data As String, ByVal fname As String, ByVal ext As String)
          Dim bytearray As Byte() = Encoding.UTF32.GetBytes(data)
          Dim base64str = Convert.ToBase64String(bytearray)
          Dim imgarr As Byte() = Convert.FromBase64String(base64str)
          Dim streams As MemoryStream = New MemoryStream(imgarr, 0, imgarr.Length)
          streams.Write(imgarr, 0, imgarr.Length)
          If Not streams Is Stream.Null Then
              Dim retimage As Image
              Try
                  retimage = Image.FromStream(streams, True, True)
              Catch ex As Exception
              End Try
          End If
      End Sub
  </code></pre>
  
  <p>Please Help me to solve this issue and save image to a folder.</p>