3
How to convert an array of bytes
for Image
in Xamarin
?
I am returning an image that is string type through a WebApi
and with that I need to make it displayed in the component Image
.
3
How to convert an array of bytes
for Image
in Xamarin
?
I am returning an image that is string type through a WebApi
and with that I need to make it displayed in the component Image
.
4
Would basically:
Image image = new Image();
Stream stream = new MemoryStream(byteArray);
image.Source = ImageSource.FromStream(() => {return stream; });
where byteArray
would be the corresponding variable byte[]
of your image.
According to the website Xamarin - Binding an Image to a byte[] Property on a model of user response Casper Nybroe:
Code
public class ByteArrayToImageSourceConverter : IValueConverter
{
public object Convert(object value,
Type targetType,
object parameter,
System.Globalization.CultureInfo culture)
{
ImageSource retSource = null;
if (value != null)
{
byte[] imageAsBytes = (byte[])value;
retSource = ImageSource.FromStream(() => new MemoryStream(imageAsBytes));
}
return retSource;
}
public object ConvertBack(object value,
Type targetType,
object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
Xaml
<ContentPage.Resources>
<ResourceDictionary>
<converters:ByteArrayToImageSourceConverter x:Key="ByteArrayToImage" />
</ResourceDictionary>
</ContentPage.Resources>
<Image Source="{Binding SelectedPollItem.Image,
Converter={StaticResource ByteArrayToImage}}" />
Reference:
Browser other questions tagged c# xamarin image
You are not signed in. Login or sign up in order to post.
Language used?
– novic
I’m using C#
– AndreeH