1
votes

I followed this guide here https://docs.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.

2
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 - Jason
@Jason Updated post with exception - MysteryMan
which line is causing that? - Jason
Could it work now ? - Leo Zhu - MSFT

2 Answers

2
votes

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;
    }
0
votes

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

}