Convert A Byte[] Array to Image in Xamarin Forms

Jestin Saji Chacko picture Jestin Saji Chacko · Dec 6, 2016 · Viewed 14.6k times · Source

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.

Answer

Ganesh Kumar picture Ganesh Kumar · May 31, 2017

(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.