1

I followed this guide here https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/photo-picker

async void OnPickPhotoButtonClicked(object sender,EventArgs e) { (sender as Image).IsEnabled = false; Stream stream = await DependencyService.Get<IPhotoPickerService>().GetImageStreamAsync(); if(stream != null) { temp_photo.Source = ImageSource.FromStream(() => stream); } (sender as Image).IsEnabled = true; } 

When I select an image using this it will show the image from the gallery in the

What I need to do is convert the image to a base64_string so that it can be sent to our server

I tried following the accepted answer from this link https://forums.xamarin.com/discussion/81344/how-to-convert-image-from-plugin-media-to-base64

var stream = file.GetStream(); var bytes = new byte [stream.Length]; await stream.ReadAsync(bytes, 0, (int)stream.Length); string base64 = System.Convert.ToBase64String(bytes); 

Adopting my method to look like this

async void OnPickPhotoButtonClicked(object sender,EventArgs e) { (sender as Image).IsEnabled = false; Stream stream = await DependencyService.Get<IPhotoPickerService>().GetImageStreamAsync(); if(stream != null) { temp_photo.Source = ImageSource.FromStream(() => stream); var bytes = new byte[stream.Length]; await stream.ReadAsync(bytes, 0, (int)stream.Length); string base64 = System.Convert.ToBase64String(bytes); } (sender as Image).IsEnabled = true; } 

But upon selecting an image this causes the application to crash

How can I convert the image selected into a base64 string?

For reference I am testing on an android device

Edit: Wrapped my method in a try block to catch the exception and this is what it says

Specified method is not supported.

4
  • the first thing you need to do with any crash is identify the exception that causes it. We can't help you until you do that. However, I will note that there is a Media plugin for XF that eliminates the need to use the DependencyService approach Commented May 6, 2020 at 21:20
  • @Jason Updated post with exception Commented May 6, 2020 at 21:30
  • which line is causing that? Commented May 6, 2020 at 21:31
  • Could it work now ? Commented May 25, 2020 at 6:23

2 Answers 2

2

Please try to copy the steam to a new MemoryStream like this:

async void OnPickPhotoButtonClicked(object sender, EventArgs e) { Stream stream = await DependencyService.Get<IPhotoPickerService>().GetImageStreamAsync(); if (stream != null) { using (MemoryStream memory = new MemoryStream()) { stream.CopyTo(memory); byte[] bytes = memory.ToArray(); image.Source = ImageSource.FromStream(() => new MemoryStream(bytes)); string base64 = System.Convert.ToBase64String(bytes); } } (sender as Button).IsEnabled = true; } 
Sign up to request clarification or add additional context in comments.

2 Comments

This stops the app from crashing but now base64 = Xamarin.Forms.StreamImageSource
@MysteryMan Couldn't you get the base64 ?I could get the base64 correctly with the method.
0

Try it.

public static class StreamExtensions { public static string ConvertToBase64(this Stream stream) { var bytes = new Byte[(int)stream.Length]; stream.Seek(0, SeekOrigin.Begin); stream.Read(bytes, 0, (int)stream.Length); return Convert.ToBase64String(bytes); } } try { var base64String = stream.ConvertToBase64(); } catch (Exception ex){ Console.WriteLine(ex.message); } 

1 Comment

This also stops the app from crashing but creates the same issue as above stated base64string = Xamarin.Forms.StreamImageSource

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.