title: Conversion of BitmapImage to Byte array
body: <p>I want to convert a BitmapImage to ByteArray in a Windows Phone 7 Application. So I tried this but it throws the runtime Exception "Invalid Pointer Exception". Can anyone explain why what I'm trying to do throws an exception. And can you provide an alternative solution for this. </p>
  
  <pre class="lang-cs prettyprint-override"><code>    public static byte[] ConvertToBytes(this BitmapImage bitmapImage)
      {
          byte[] data;
          // Get an Image Stream
          using (MemoryStream ms = new MemoryStream())
          {
              WriteableBitmap btmMap = new WriteableBitmap(bitmapImage);
  
              // write an image into the stream
              Extensions.SaveJpeg(btmMap, ms,
                  bitmapImage.PixelWidth, bitmapImage.PixelHeight, 0, 100);
  
              // reset the stream pointer to the beginning
              ms.Seek(0, 0);
              //read the stream into a byte array
              data = new byte[ms.Length];
              ms.Read(data, 0, data.Length);
          }
          //data now holds the bytes of the image
          return data;
      }
  </code></pre>