Conversion of BitmapImage to Byte array

前端 未结 3 848
悲哀的现实
悲哀的现实 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:26

    I'm not sure exactly what your problem is, but I know that the following code is a very minor change from code that I know works (mine was passing in a WriteableBitmap, not a BitmapImage):

    public static byte[] ConvertToBytes(this BitmapImage bitmapImage)
    {
        byte[] data = null;
        using (MemoryStream stream = new MemoryStream())
        {
            WriteableBitmap wBitmap = new WriteableBitmap(bitmapImage);
            wBitmap.SaveJpeg(stream, wBitmap.PixelWidth, wBitmap.PixelHeight, 0, 100);
            stream.Seek(0, SeekOrigin.Begin);
            data = stream.GetBuffer();
        }
    
        return data;
    }

提交回复
热议问题