title: Convert Image to Base64 string
body: <p>I am trying to convert an Image file to Base64 string in UWP with <code>StorageFile</code>
  Here's the method I have:</p>
  
  <pre><code>public async Task&lt;string&gt; ToBase64String(StorageFile file)
  {
      var stream = await file.OpenAsync(FileAccessMode.Read);
      var decoder = await BitmapDecoder.CreateAsync(stream);
      var pixels = await decoder.GetPixelDataAsync();
      var bytes = pixels.DetachPixelData();
      return await ToBase64(bytes, (uint)decoder.PixelWidth, (uint)decoder.PixelHeight, decoder.DpiX, decoder.DpiY);
  }
  </code></pre>
  
  <p>and <code>ToBase64</code> goes like this:</p>
  
  <pre><code>private async Task&lt;string&gt; ToBase64(byte[] image, uint pixelWidth, uint pixelHeight, double dpiX, double dpiY)
  {
      //encode
      var encoded = new InMemoryRandomAccessStream();
      var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, encoded);
      encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, pixelWidth, pixelHeight, dpiX, dpiY, image);
      await encoder.FlushAsync();
      encoded.Seek(0);
  
      //read bytes
      var bytes = new byte[encoded.Size];
      await encoded.AsStream().ReadAsync(bytes, 0, bytes.Length);
  
      return System.Convert.ToBase64String(bytes);
  }
  </code></pre>
  
  <p>and calling it from my MainPage.xaml.cs</p>
  
  <pre><code>StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
  string square_b64 = converter.ToBase64String(await storageFolder.GetFileAsync("Image.png")).Result;
  </code></pre>
  
  <p>However, This is not working. Any Heads up?</p>