位图图像转换为字节数组

14

我想在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;
    }
3个回答

18

我可以让你已有的代码变得更简单:

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

......但那可能解决不了问题。

另一个问题是,你只使用了bitmapImage大小——你不应该在某个时刻将其复制到btmMap上吗?

你为什么不直接使用这个:

WriteableBitmap btmMap = new WriteableBitmap(bitmapImage);

你能否提供有关错误发生位置的更多信息?


实际上,我已经使用了上面的代码:WriteableBitmap btmMap = new WriteableBitmap(bitmapImage); 之前我表述错误,但它仍然显示相同的错误:“无效指针”。 - dinesh
2
当我尝试使用你的方法时,除非我在构造函数中使用BitmapImage将btmMap初始化为WritableBitmap,否则我最终会得到一张黑色的图片。我不确定是否缺少某种设置,但我想提一下。 - Tom Kidd
你能否建议一种在Windows 8 RT中实现它的方法? - Mayank
你能告诉我如何将byte[]转换为BitmapImage吗? - Leo Nguyen
3
我尚未尝试过,但你是否尝试创建一个“BitmapImage”,然后将“StreamSource”设置为包装字节数组的“MemoryStream”? - Jon Skeet

7

我不确定你的问题具体是什么,但我知道以下代码与我知道的有效代码只有非常微小的变化(我的代码传递的是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,这会给我带来问题吗? - dinesh
我不明白为什么不行;除非你在尝试从图像中获取byte[]之前没有等待图像实际下载完成。 - Derek Lakin
1
现在我找到了解决方案,问题是无法创建WritableBitmap对象,因为BitmapImage对象是使用相对URI创建的。所以它没有获取真实图像。所以我已经更改了代码:不是这样写WriteableBitmap wBitmap = new WriteableBitmap(bitmapImage);我们必须执行以下操作... Image image = new Image(); image.SetSource(bitmapImage) ;WriteableBitmap wBitmap = new WriteableBitmap(image,null);现在我可以创建WritableBitmap对象了。 - dinesh
@dinesh 我遇到了相同的问题:无效的指针。但我没能解决这个问题?我使用 uri 创建位图,即“objBitmapImage.UriSource = (new Uri(e.OriginalFileName));”,但在将图像写入流数组时,我收到了空指针异常。 "System.Windows.Media.Imaging.Extensions.SaveJpeg(new WriteableBitmap(bm_Image), ms_Image, 320, 320, 0, 100);//here i get the null pointer exception
我尝试了您的代码,但我找不到Image.SetSource(bitmapImage),而是有image.source。你能帮帮我吗?
- Sujiz
我正在使用你在问题中提到的相同代码。你在这里提到,而不是WrietableBitmap(image,null)...(在你的最后一条评论中提到的)正在工作。但是它是如何工作的?不需要写入内存流吗? - Sujiz

1
我遇到了同样的问题,这个解决了它: 之前的代码:
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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接