title: Convert a base64string back to byte[]
body: <p>I want to use C# to convert a <code>byteArray</code> to <code>base64string</code> by using <code>Convert.ToBase64String()</code>, then I want to send this string to save using MySQL by sending post data to a PHP page. My problem is I can't convert this string back to a <code>byteArray</code> using this method as after this string is retrieved from the PHP page (after it got data from MySQL), it tells me that argument on method <code>Convert.FromBase64String()</code> was wrong.</p>
  
  <p>I don't where is problem occurs, how can I solve it?</p>
  
  <p>My code:</p>
  
  <pre><code>public static string BitmapToString(BitmapImage img)
  {
      try
      {
          WriteableBitmap bmp = new WriteableBitmap(img);
          byte[] byteArray = null;
          string str = null;
          MemoryStream stream = new MemoryStream();
          bmp.SaveJpeg(stream, bmp.PixelWidth, bmp.PixelHeight, 0, 100);
          byteArray = stream.ToArray();
          str = Convert.ToBase64String(byteArray);
          return str;
      }
      catch (System.Exception ex)
      {
          Console.WriteLine(ex.StackTrace);
      }
      return null;
  }
  
  public static BitmapImage StringToBitmap(string str)
  {
      try
      {
          byte[] byteArray = Convert.FromBase64String(str);
          Stream memStream = new MemoryStream(byteArray);
          BitmapImage img = null;
          MemoryStream stream = new MemoryStream(byteArray);
          stream.Seek(0, SeekOrigin.Begin);
          img = new BitmapImage();
          img.SetSource(stream);
          return img;
      }
      catch (System.Exception ex)
      {
          Console.WriteLine(ex.StackTrace);
      }
      return null;
  }
  </code></pre>