9

Before asking this question I googled a lot but couldn't find a solution that suits mine.

In Xamarin.Forms I have a byte[] array and I want to Convert that byte[] array to an Image. How can I achieve that, this is what I tried:

In Front End(XAML):

<StackLayout BackgroundColor="Olive" x:Name="imagePanel"> <Image x:Name="PdfImage" Aspect="AspectFill" IsVisible="true"/> </StackLayout> 

In Code Behind(C#):

byte[] imageAsBytes = Constant.jsonPDF; var stream1 = new MemoryStream(imageAsBytes); PdfImage.Source = ImageSource.FromStream(() => new MemoryStream(imageAsBytes)); imagePanel.Children.Add(PdfImage); 

But My problem is image is not displaying.

Can anybody tell me what I'm doing wrong. Any help would be greatly appreciated.

9
  • is the byte[] a jpg or png? Commented Dec 6, 2016 at 19:24
  • Actually that i don't know... Constant.jsonPdf contains the value and is coming from server. When i debug i can only see the byte[694753]. Commented Dec 6, 2016 at 19:54
  • 1
    you should verify that - write it to disk and then look at it with an image viewer, or download it to your desktop with curl or your browser. If it's not a valid image type it won't display in the Image control Commented Dec 6, 2016 at 20:37
  • Ok Thanks... I Saved the byte array to gallery using DependencyService but i'm only getting a blank screen ;( Commented Dec 7, 2016 at 12:43
  • it sounds like it's either a bad image, or in some unsupported format Commented Dec 8, 2016 at 14:56

3 Answers 3

12

(XAML):

 <Image Grid.Row="1" x:Name="IncidentImageData" Grid.ColumnSpan="4" BackgroundColor="DarkGray" Aspect="AspectFill" WidthRequest="50" HeightRequest="175"/> 

viewModel.SImageBase64 is a byte[]

Code Behind(C#):

var stream1 = new MemoryStream(viewModel.SImageBase64); IncidentImageData.Source = ImageSource.FromStream(() => stream1); 

simply i have done like this and image has shown.

Sign up to request clarification or add additional context in comments.

2 Comments

This doesn't work and is spoken about hundreds of times on the Xamarin Github and Microsoft Devs pages because ImageSource.FromStream() requires a brand new MemoryStream to be created within it's lambda function. You're very lucky if this worked for you but 9 times out of 10 this solution won't work.
The slightly more long winded, but working way of doing it similarly to this is by converting the MemoryStream to a byte array and then using that byte array to create another MemoryStream within the lambda function of FromStream()
8

A Potential Fix

I know this thread is 2 years old now but I thought i'd post a working solution here for anyone who's also struggling with this. Just spent half a day researching and trying to solve an identical problem, the way the code was written on this post helped me greatly as it is almost 100% correct. You just need to provide the MemoryStream as a return from a lambda function within the FromStream() method.

Change this:

PdfImage.Source = ImageSource.FromStream(() => new MemoryStream(imageAsBytes)); 

To:

PdfImage.Source = ImageSource.FromStream(() => { return new MemoryStream(imageAsBytes); }); 

And the snippet should be working as of Xamarin Forms 5.0.0.2012

Full Code:

byte[] imageAsBytes = Constant.jsonPDF; PdfImage.Source = ImageSource.FromStream(() => { return new MemoryStream(imageAsBytes); }); imagePanel.Children.Add(PdfImage); 

9 Comments

Thanks for your solution it did work really good. I just had to change ´imagePanel.Children.Add(PdfImage)´ to ´PdfImage.Source = ImageSource.FromStream(() => { return new MemoryStream(qrCodeAsBitmapByteArr); });´. Without your answer I wouldn't be able success. Thanks a lot. EDIT: My bad, you already did that and i overlooked it.
no worries! if this helped then be sure to mark the thread as solved!
I am not the OP but I gave you an upvote. :D
@MatthewSwallow , stream1 is no longer needed
Could have simplified like - PdfImage.Source = ImageSource.FromStream(() => new MemoryStream(imageAsBytes));
|
4

Use this Code :

imgUserImage.Source = ImageSource.FromStream(() => new MemoryStream(userList.Single().ProfilePhoto)); 

Here profile photo type is byte[]

public byte[] ProfilePhoto { get; set; } 

1 Comment

Code-only answers are discouraged. Please click on edit and add some words summarising how your code addresses the question, or perhaps explain how your answer differs from the previous answer/answers. From Review

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.