简体   繁体   中英

Xamarin Forms Byte[] to Image Source not showing

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

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);
            }         

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM