title: C# Windows 8 Store (Metro, WinRT) Byte array to BitmapImage
body: <p>I am working on a Windows 8 Metro app that applies filters to images. I have a web version of the app and wanted to port it. But as we all know WinRT doesn't have all the good things .NET provides otherwise :/</p>
  <p>Currently I am applying the filters on a byte array and I want to keep it that way, because it's super fast! So for the past few days I have been searching for ways to convert a StorageFile to byte[] and then byte[] to BitmapImage.</p>
  <p>So far I have managed to do the first one (StorageFile to byte[]). Here is how I do it:</p>
  <pre><code>public async Task&lt;Byte[]&gt; ImageFileToByteArray(StorageFile file)
      {
          IRandomAccessStream stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
          BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
          PixelDataProvider pixelData = await decoder.GetPixelDataAsync();
          return pixelData.DetachPixelData();
      }
  </code></pre>
  <p>This piece of code returns a <code>byte[]</code> that contains the pixel data as BGRA.</p>
  <p>And here comes the tricky part. I cannot successfully convert the byte array into a BitmapImage. I have searched all over the places and many people suggest using WriteableBitmap but that doesn't do much good to me. I have also found some pieces of code that should be working... but they don't.</p>
  <p>One of the solutions I have tried is using InMemoryRandomAccessStream like this:</p>
  <pre><code>public async Task&lt;BitmapImage&gt; ByteArrayToBitmapImage(Byte[] pixels)
      {
          var stream = new InMemoryRandomAccessStream();
          await stream.WriteAsync(pixels.AsBuffer());
          stream.Seek(0);
          var image = new BitmapImage();
          await image.SetSourceAsync(stream);
          return image;
      }
  </code></pre>
  <p>This one throws the following exception:</p>
  <blockquote>
  <p>An exception of type 'System.Exception' occurred in mscorlib.dll but was not handled in user code</p>
  <p>Additional information: The component cannot be found. (Exception from HRESULT: 0x88982F50)</p>
  </blockquote>
  <p>I tried using this line instead:</p>
  <pre><code>PixelDataProvider pixelData = await decoder.GetPixelDataAsync(
              BitmapPixelFormat.Bgra8, 
              BitmapAlphaMode.Ignore, 
              new BitmapTransform(),
              ExifOrientationMode.IgnoreExifOrientation, 
              ColorManagementMode.DoNotColorManage);
  </code></pre>
  <p>But it did me no good since I keep getting that exception.</p>
  <p>I have also tried this:</p>
  <pre><code>var bitmapImage = new BitmapImage();
          var pixels = await ImageFileToByteArray(file);
          ImageSource imgSource;
          using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
          {
              using (DataWriter writer = new DataWriter(ms.GetOutputStreamAt(0)))
              {
                  writer.WriteBytes(pixels);
                  await writer.StoreAsync();
              }
  
              await bitmapImage.SetSourceAsync(ms);
              imgSource = bitmapImage;
          }
  </code></pre>
  <p>And get the same exception as the first piece of code.</p>
  <p>I have also tried several other ways that include using a normal Stream then converting into a IRandomAccessStream but they didn't work either.</p>
  <p>All of the above code seems fine to me. So my guess at the moment is that the problem is in the <code>byte[]</code>. I'm guessing that the format of the pixelData inside is not valid, so I tried changing it to RGBA but that didn't help either. Also the PixelHeight and PixelWidth of the BitmapImage are 0.</p>