14

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;
    }
Emil Vikström
  • 90,431
  • 16
  • 141
  • 175
dinesh
  • 635
  • 1
  • 10
  • 23

3 Answers3

18

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?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Actually, I have used the above thing, WriteableBitmap btmMap = new WriteableBitmap(bitmapImage); Previously I have shown the wrong thing. but Still It shows the same error " Invalid Pointer". – dinesh Jan 19 '11 at 08:03
  • 2
    When I try to use your method, I wind up getting a black image unless I initialize btmMap to a WritableBitmap using the BitmapImage in the constructor. I'm not sure if I'm missing some sort of setup but I thought I'd mention it. – Tom Kidd Jan 19 '12 at 22:53
  • can you please suggest a way to do it in Windows 8 RT? – Mayank Oct 18 '12 at 20:04
  • Can you tell me how to convert byte[] to BitmapImage? – Leo Nguyen Sep 14 '13 at 03:07
  • 3
    @LeoNguyễn: I haven't tried it, but have you tried creating a `BitmapImage` and then setting the `StreamSource` to a `MemoryStream` wrapping the byte array? – Jon Skeet Sep 14 '13 at 08:17
7

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;
}
Eugene
  • 2,965
  • 2
  • 34
  • 39
Derek Lakin
  • 16,179
  • 36
  • 51
  • I have created bitmapImage using URI, is this creating a problem for me ? – dinesh Jan 19 '11 at 11:55
  • I didn't see why it should no; unless you're not waiting until the image has actually downloaded before you try and get the byte[] from it. – Derek Lakin Jan 19 '11 at 11:57
  • 1
    Now I got the solution, the problem is unable to create WritableBitmap object because, BitmapImage object is created using URI which is relative. so It does not get the real Image. so I had changed the code : Insted of this WriteableBitmap wBitmap = new WriteableBitmap(bitmapImage); We have to do the following... Image image = new Image(); image.SetSource(bitmapImage) ;WriteableBitmap wBitmap = new WriteableBitmap(image,null); Now I can able to create the WritableBitmap object – dinesh Jan 19 '11 at 13:39
  • @dinesh I have the same issue invalid pointer .But i failed to solve the issue? i created the bitmap using uri ie "objBitmapImage.UriSource = (new Uri(e.OriginalFileName));" and i get null pointer exception during writing the image to stream arrray " System.Windows.Media.Imaging.Extensions.SaveJpeg(new WriteableBitmap(bm_Image), ms_Image, 320, 320, 0, 100);//here i get the null pointer exception I tried your code but i cant find Image.SetSource(bitmapImage),instead of it image.source is there.Can u please help me – Sujiz Oct 19 '11 at 09:22
  • I using the same code that you asked in the question. u mention here that instead of it WrietableBitmap(image,null)..(mentioned in your last comment) is working.but how it is work?dont need to write to memory stream? – Sujiz Oct 19 '11 at 09:38
1

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);
Eugene
  • 2,965
  • 2
  • 34
  • 39
kober
  • 832
  • 7
  • 13