question

coldstarjoey-7325 avatar image
0 Votes"
coldstarjoey-7325 asked JessieZhang-2116 commented

Image source showing nothing when setting the source from a byte[] Xamarin Forms

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

dotnet-csharpdotnet-xamarinforms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JessieZhang-2116 avatar image
0 Votes"
JessieZhang-2116 answered JessieZhang-2116 commented

Hello,


Welcome to our Microsoft Q&A platform!

Here is a sample which is implemented by Xamarin.Essentials: Media Picker.

You can refer to the following code:

     async void Button_Clicked(System.Object sender, System.EventArgs e)
     {
         var result = await MediaPicker.PickPhotoAsync(new MediaPickerOptions
         {
             Title = "Please pick a photo"
         });

         if (result != null)
         {
             var stream = await result.OpenReadAsync();

             resultImage.Source = ImageSource.FromStream(() => stream);
         }
     }

The xaml code:

 <Image x:Name="resultImage" />

And you can check the full sample here: https://github.com/jfversluis/XFEMediaPickerSample


Update:

We just need to use the following code, the rest can be deleted(I tested on my side, it works properly.):

     async Task LoadPhotoAsync(FileResult photo)
     {
         // canceled
         if (photo == null)
         {
             PhotoPath = null;
             return;
         }
         // save the file into local storage
         var newFile = photo.FullPath;

         var stream = await photo.OpenReadAsync();

         MyImage.Source = ImageSource.FromStream(() => stream);
     }
 }


Best Regards,

Jessie Zhang


If the response is helpful, please click "Accept Answer" and upvote it.


Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@JessieZhang-2116 unfortunately I am doing this after storing a byte[] in a local DB on the device this is for using the image straight away after picking it from the device, I have a listview of images that I save to the local DB as a Byte[] and when the user clicks on the list item I want it to show the image on another screen that's where it is not loading.

0 Votes 0 ·

If it is convenient for you , could you please post a basic demo so that we can test on our side?

0 Votes 0 ·

here is my github for a standard button to pick and save the image as a byte[] then set the image source using a stream, I'm still getting the same results. https://github.com/ColdstarJoey/CameraByteTest

0 Votes 0 ·
Show more comments

I have updated my answer, you can check the updated part at the bottom of my answer.

0 Votes 0 ·