Преобразование BitmapImage в массив байтов

Я хочу преобразовать BitmapImage в ByteArray в приложении Windows Phone 7. Итак, я попробовал это, но он выдает исключение времени выполнения «Invalid Pointer Exception». Может ли кто-нибудь объяснить, почему то, что я пытаюсь сделать, вызывает исключение. И можете ли вы предоставить для этого альтернативное решение.

    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;
    }
13
задан Emil Vikström 23 June 2012 в 11:06
поделиться