Windows Phone 8.0 Blob to Image

0

I have a Web service which is connected to a DB, I need to pull out a type BLOB from that BD through the web service and then create an Image. The web service send a bytes array from the blob and I tried with a converter bind to the image in XML (like some suggestions I found) like this

 using (Stream ms = new MemoryStream(fileBytes, 0, fileBytes.Length))
        {  
            ms.Write(fileBytes, 0, fileBytes.Length);
            ms.Seek(0, SeekOrigin.Begin);
            BitmapImage bitmapImage = new BitmapImage();
            ////////////////Exception here
            bitmapImage.SetSource(ms);
            return bitmapImage;
        }

But then I get this error (Exception from HRESULT: 0x88982F50). Then I found that maybe could be the format of the array of bytes the problem, that maybe could be in base64 so I've changed the web service and now it sends me an 64-Base String and then changed to the array of bytes, but still with the same error.

I found this post but still no luck. Then I've found a lot of solutions like InMemoryRandomAccessStream or Image.FromStream but those solutions are for WP 8.1

Also I used:

    image.SaveJpeg(stream, image.PixelWidth, image.PixelHeight, 0, 90);
    ///////AND////////
    image = PictureDecoder.DecodeJpeg(ms);

but still no luck

I'm sure that I'm missing something because every solution point out to these solutions but I really can't figure it out.

c#
.net
windows-phone-8
windows-phone
asked on Stack Overflow Jun 12, 2014 by killemmanuel • edited Jun 12, 2014 by ekremkaraca

2 Answers

0

I accomplish this (loading a binary object from database into an image) using the following function:

public static BitmapImage LoadImage(byte[] imageData)
{
    if (imageData == null || imageData.Length == 0) return null;
    var image = new BitmapImage();
    using (var mem = new MemoryStream(imageData))
    {
        mem.Position = 0;
        image.BeginInit();
        image.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
        image.CacheOption = BitmapCacheOption.OnLoad;
        image.UriSource = null;
        image.StreamSource = mem;
        image.EndInit();
    }
    image.Freeze();
    return image;
}

As you can see, the function takes a byte[] array as a parameter. I get this array from the database like this:

using (var reader = dataLayer.ExecuteReader(sqlString))
{
    if (reader.Read())
    {
        if (reader["ImageData"] != null && reader["ImageData"] != DBNull.Value)
        {
            imageBytes = (byte[])reader["ImageData"];
        }
    }
}

where ImageData is the column name from the database. The LoadImage() function is then called with the imageBytes passed in as a parameter.

answered on Stack Overflow Jun 12, 2014 by dub stylee
0

I finally get it, it was a error with the DB actually. The data on the column I was getting the array of bytes was corrupt or has another use that I don't understand, so I contact with the DB administrator and he helped me with that so now I have the correct data. the first code really works. Thanks to everyone for the help

answered on Stack Overflow Jun 20, 2014 by killemmanuel

User contributions licensed under CC BY-SA 3.0