BitmapImage的转换为字节数组 [英] Conversion of BitmapImage to Byte array

查看:173
本文介绍了BitmapImage的转换为字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个BitmapImage的到的ByteArray转换在Windows Phone 7应用程序。所以,我想这一点,但它引发的运行时异常无效指针异常。任何人都可以解释为什么我想要做的抛出异常。你也可以提供这样的一个替代解决方案。

公共静态的byte [] ConvertToBytes(此的BitmapImage的BitmapImage)
    {
        字节[]数据;
        //获取的图像流
        使用(MemoryStream的毫秒=新的MemoryStream())
        {
            WriteableBitmap的btmMap =新的WriteableBitmap的(的BitmapImage);            //写一个图像到流
            Extensions.SaveJpeg(btmMap,MS,
                bitmapImage.PixelWidth,bitmapImage.PixelHeight,0,100);            //流指针复位到初
            ms.Seek(0,0);
            //读取流成字节数组
            数据=新的字节[ms.Length]
            ms.Read(数据,0,data.Length);
        }
        //现在数据保存图像的字节
        返回的数据;
    }


解决方案

好吧,我可以让code你已经有了相当简单:

 公共静态的byte [] ConvertToBytes(此的BitmapImage的BitmapImage)
{
    使用(MemoryStream的毫秒=新的MemoryStream())
    {
        WriteableBitmap的btmMap =新的WriteableBitmap的
            (bitmapImage.PixelWidth,bitmapImage.PixelHeight);        //写一个图像到流
        Extensions.SaveJpeg(btmMap,MS,
            bitmapImage.PixelWidth,bitmapImage.PixelHeight,0,100);        返回ms.ToArray();
    }
}

...但是,这可能不会解决问题。

另一个问题是,你永远只能使用的尺寸的BitmapImage 的 - 你不应该被复制到该 btmMap ?

有没有你不只是使用这个任何原因:

  WriteableBitmap的btmMap =新的WriteableBitmap的(的BitmapImage);

你能不能给我们介绍一下发生错误的更多信息?

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.

    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?

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?

这篇关于BitmapImage的转换为字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆