Conversion of BitmapImage to Byte array

前端 未结 3 851
悲哀的现实
悲哀的现实 2020-11-30 13:55

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

3条回答
  •  天涯浪人
    2020-11-30 14:17

    Well I can make the code you've got considerably simpler:

    public static byte[] ConvertToBytes(this BitmapImage bitmapImage)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            WriteableBitmap btmMap = new WriteableBitmap
                (bitmapImage.PixelWidth, bitmapImage.PixelHeight);
    
            // write an image into the stream
            Extensions.SaveJpeg(btmMap, ms,
                bitmapImage.PixelWidth, bitmapImage.PixelHeight, 0, 100);
    
            return ms.ToArray();
        }
    }
    

    ... but that probably won't solve the problem.

    Another issue is that you're only ever using the size of bitmapImage - shouldn't you be copying that onto btmMap at some point?

    Is there any reason you're not just using this:

    WriteableBitmap btmMap = new WriteableBitmap(bitmapImage);
    

    Can you give us more information about where the error occurs?

提交回复
热议问题