简体   繁体   English

将BitmapImage转换为Byte数组

[英]Conversion of BitmapImage to Byte array

I want to convert a BitmapImage to ByteArray in a Windows Phone 7 Application. 我想在Windows Phone 7应用程序中将BitmapImage转换为ByteArray。 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. 您能为此提供替代解决方案吗?

    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;
    }

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? 另一个问题是你只使用了bitmapImage大小 - 你不应该在某些时候将它复制到btmMap吗?

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? 您能否向我们提供有关错误发生位置的更多信息?

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): 我不确定你的问题到底是什么,但我知道下面的代码是一个非常小的变化,我知道的代码是有效的(我的是在WriteableBitmap中传递,而不是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;
}

I had same problem, this solves it: 我有同样的问题,这解决了它:

Code before: 代号:

BitmapImage bi = new BitmapImage();
bi.SetSource(e.ChosenPhoto);
WriteableBitmap wb = new WriteableBitmap(bi);

Code after: 代码后:

BitmapImage bi = new BitmapImage();
bi.CreateOptions = BitmapCreateOptions.None;
bi.SetSource(e.ChosenPhoto);
WriteableBitmap wb = new WriteableBitmap(bi);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM