title: Load, show, convert image from byte array (database) in Windows Phone 8.1
body: <p>The full question is how to show image loaded from database in windows phone 8.1.
  Image data is loaded as byte array (checked - loads fine).</p>
  
  <p>Displaying image by specifying urisource works fine.</p>
  
  <pre><code>Image img = new Image();
  img.Source = new BitmapImage() {UriSource = new Uri("http://www.example.com/1.jpg") };
  rootgrid.Children.Add(img);    
  </code></pre>
  
  <p>But when byte array (of image) is converted to BitmapImage - nothing is shown.
  The only exception free example I'v found so far is:</p>
  
  <pre><code>public BitmapImage ConvertToBitmapImage(byte[] image)
  {
      InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream();
      var bitmapImage = new BitmapImage();
      var memoryStream = new MemoryStream(image);
      memoryStream.CopyToAsync(ras.AsStreamForWrite());
      bitmapImage.SetSourceAsync(ras);
      return bitmapImage;
  }
  
  Image img = new Image();
  img.Source = ConvertToBitmapImage(picturebytearray);
  rootgrid.Children.Add(img);
  </code></pre>
  
  <p>But no picture is shown then.</p>
  
  <p>Microsoft's documentation contains example only of image loading from stream that is obtained by opening file from internal storage. But I need to load image, that is saved in sqlite database.
  Image data is in jpeg format.</p>
  
  <p>Edit:
  Working code based on <a href="https://stackoverflow.com/users/1081079/freshbm">freshbm</a> solution:</p>
  
  <pre><code>public async Task&lt;BitmapImage&gt; ConvertToBitmapImage(byte[] image)
  {
      BitmapImage bitmapimage = null;
      using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
      {
          using (DataWriter writer = new DataWriter(ms.GetOutputStreamAt(0)))
          {
              writer.WriteBytes((byte[])image);
              await writer.StoreAsync();
          }
          bitmapimage = new BitmapImage();
          bitmapimage.SetSource(ms);
      }
      return bitmapimage;
  }
  </code></pre>
  
  <p>Then in constructors you can use:</p>
  
  <pre><code>img.Source = ConvertToBitmapImage(imagebytearray).Result;
  </code></pre>
  
  <p>or else</p>
  
  <pre><code>img.Source = await ConvertToBitmapImage(imagebytearray);
  </code></pre>