【问题标题】:Conversion of BitmapImage to Byte arrayBitmapImage 到 Byte 数组的转换
【发布时间】:2011-01-19 07:35:26
【问题描述】:

我想在 Windows Phone 7 应用程序中将 BitmapImage 转换为 ByteArray。所以我尝试了这个,但它抛出了运行时异常“无效指针异常”。谁能解释为什么我正在尝试做的事情会引发异常。您能否为此提供替代解决方案。

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

【问题讨论】:

    标签: c# windows-phone-7


    【解决方案1】:

    好吧,我可以让你的代码变得相当简单:

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

    ...但这可能无法解决问题。

    另一个问题是您只使用过bitmapImagesize - 您不应该在某个时候将其复制到btmMap 上吗?

    你有什么理由不只是使用这个:

    WriteableBitmap btmMap = new WriteableBitmap(bitmapImage);
    

    您能否向我们提供有关错误发生位置的更多信息?

    【讨论】:

    • 其实上面的东西我都用过,WriteableBitmap btmMap = new WriteableBitmap(bitmapImage);以前我已经展示了错误的东西。但仍然显示相同的错误“无效指针”。
    • 当我尝试使用你的方法时,我最终得到一个黑色图像,除非我使用构造函数中的 BitmapImage 将 btmMap 初始化为 WritableBitmap。我不确定我是否缺少某种设置,但我想我会提到它。
    • 您能否建议一种在 Windows 8 RT 中执行此操作的方法?
    • 你能告诉我如何将 byte[] 转换为 BitmapImage 吗?
    • @LeoNguyễn:我还没有尝试过,但是您是否尝试过创建BitmapImage,然后将StreamSource 设置为包装字节数组的MemoryStream
    【解决方案2】:

    我不确定您的问题到底是什么,但我知道以下代码与我知道有效的代码相比 非常 微小的变化(我的代码传入的是 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;
    }

    【讨论】:

    • 我使用 URI 创建了 bitmapImage,这会给我带来问题吗?
    • 我不明白为什么它不应该;除非您没有等到图像实际下载后才尝试从中获取 byte[]。
    • 现在我得到了解决方案,问题是无法创建 WritableBitmap 对象,因为 BitmapImage 对象是使用相对的 URI 创建的。所以它没有得到真实的图像。所以我改变了代码: Insted of this WriteableBitmap wBitmap = new WriteableBitmap(bitmapImage);我们必须执行以下操作... Image image = new Image(); image.SetSource(bitmapImage) ;WriteableBitmap wBitmap = new WriteableBitmap(image,null);现在我可以创建 WritableBitmap 对象
    • @dinesh 我有同样的问题无效指针。但我没能解决这个问题?我使用 uri 创建了位图,即“objBitmapImage.UriSource = (new Uri(e.OriginalFileName));”并且我在将图像写入流阵列时出现空指针异常“ System.Windows.Media.Imaging.Extensions.SaveJpeg(new WriteableBitmap(bm_Image), ms_Image, 320, 320, 0, 100);//这里我得到了 null指针异常我试过你的代码,但我找不到 Image.SetSource(bitmapImage),而不是它 image.source 在那里。你能帮我吗
    • 我使用您在问题中提出的相同代码。你在这里提到,而不是它 WrietableBitmap(image,null)..(在你上一条评论中提到)正在工作。但是它是如何工作的?不需要写入内存流?
    【解决方案3】:

    我遇到了同样的问题,这个解决了:

    之前的代码:

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

    之后的代码:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多