0
votes

Hi all I have a byte[] which I save from the Xamarin Essentials Media Picker, I want to display the image on my XAML page so all I have is a blank XAML page with a StackLayout and in my code I do the following:

Stream stream = new MemoryStream(filebyte);
var imageSource = ImageSource.FromStream(() => stream);

Image test = new Image();

test.Source = imageSource;

test.WidthRequest = 50;
test.HeightRequest = 50;

ImageStack.Children.Add(test);

but when the page loads nothing is there. i'm I missing something. I have even tried this on a fresh project using the latest version of Xamarin Forms and Xamarin Essential's

1
Try var imageSource = ImageSource.FromStream(() => new MemoryStream(filebyte)); your factory method returns disposed stream, when it is asked for it.Alexey Rumyantsev
@AlexeyRumyantsev getting the same problem when doing it that wayJoey Ireland

1 Answers

0
votes

imgByteArray is Byte[], using following code to add image by behind code.

            var stream1 = new MemoryStream(imgByteArray);
            image.Source = ImageSource.FromStream(() => stream1);
          
            image.WidthRequest = 50;
                image.HeightRequest = 50;
                imagestack.Children.Add(image);

Update:

Adding one image in Forms, setting Build action as Embedded resource, then converting this image into byte[], using this byte[] for Image to show. I have no problem.

 var image = new Xamarin.Forms.Image();

        var assembly = this.GetType().GetTypeInfo().Assembly;
        byte[] imgByteArray = null;

        var s = assembly.GetManifestResourceStream("FormsSample.f19.png");
        
            if (s != null)
            {
                var length = s.Length;
                imgByteArray = new byte[length];
                s.Read(imgByteArray, 0, (int)length);              
            var stream1 = new MemoryStream(imgByteArray);
            image.Source = ImageSource.FromStream(() => stream1);
           
            image.WidthRequest = 50;
                image.HeightRequest = 50;
                imagestack.Children.Add(image);
            }