9

I am working on Windows 8 store application. I am new at it.

I am receiving an image in the form of byte array (byte []).

I have to convert this back to Image and display it in Image Control.

so far I have button and Image control on Screen. When I click button, I call following function

private async Task LoadImageAsync()
{
    byte[] code = //call to third party API for byte array
    System.IO.MemoryStream ms = new MemoryStream(code);
    var bitmapImg = new Windows.UI.Xaml.Media.Imaging.BitmapImage();

    Windows.Storage.Streams.InMemoryRandomAccessStream imras = new Windows.Storage.Streams.InMemoryRandomAccessStream();

    Windows.Storage.Streams.DataWriter write = new Windows.Storage.Streams.DataWriter(imras.GetOutputStreamAt(0));
    write.WriteBytes(code);
    await write.StoreAsync();
    bitmapImg.SetSourceAsync(imras);
    pictureBox1.Source = bitmapImg;
}

This is not working properly. any idea? When I debug, I can see the byte array in ms. but it is not getting converted to bitmapImg.

Developer
  • 183
  • 2
  • 2
  • 10

3 Answers3

15

I found the following on Codeproject

public class ByteImageConverter
{
    public static ImageSource ByteToImage(byte[] imageData)
    {
        BitmapImage biImg = new BitmapImage();
        MemoryStream ms = new MemoryStream(imageData);
        biImg.BeginInit();
        biImg.StreamSource = ms;
        biImg.EndInit();

        ImageSource imgSrc = biImg as ImageSource;

        return imgSrc;
    }
}

This code should work for you.

Community
  • 1
  • 1
Tomtom
  • 9,087
  • 7
  • 52
  • 95
  • Hi Tom! This solution is for WPF. I cannot find BefinInit() method in BitmapImage class in Windows Store Application (http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.media.imaging.bitmapimage.aspx) – Developer Feb 27 '14 at 11:11
4

You can try something like that:

public object Convert(object value, Type targetType, object parameter, string language)
{
    byte[] rawImage = value as byte[];

    using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
    {
        using (DataWriter writer = new DataWriter(ms.GetOutputStreamAt(0)))
        {
            writer.WriteBytes((byte[])rawImage);

            // The GetResults here forces to wait until the operation completes
            // (i.e., it is executed synchronously), so this call can block the UI.
            writer.StoreAsync().GetResults();
        }

        BitmapImage image = new BitmapImage();
        image.SetSource(ms);
        return image;
    }
}
Francesco Bonizzi
  • 5,142
  • 6
  • 49
  • 88
1

I found the following answer in another thread (Image to byte[], Convert and ConvertBack). I used this solution in a Windows Phone 8.1 project, not sure about Windows Store apps, but I believe it will work.

public object Convert(object value, Type targetType, object parameter, string culture)
    {
        // Whatever byte[] you're trying to convert.
        byte[] imageBytes = (value as FileAttachment).ContentBytes;

        BitmapImage image = new BitmapImage();
        InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream();
        ms.AsStreamForWrite().Write(imageBytes, 0, imageBytes.Length);
        ms.Seek(0);

        image.SetSource(ms);
        ImageSource src = image;

        return src; 
    }
Community
  • 1
  • 1
Joe Martella
  • 722
  • 1
  • 8
  • 19