1

I want to take the new implementation of taking a photo then converting it into a byte[] using the preview version of Xamarin Essentials

See below the code I have to take the image then load it.

async Task TakePhotoAsync()
{
    try
    {
        var photo = await MediaPicker.CapturePhotoAsync();
        await LoadPhotoAsync(photo);
        Console.WriteLine($"CapturePhotoAsync COMPLETED: {PhotoPath}");
    }
    catch (Exception ex)
    {
        Console.WriteLine($"CapturePhotoAsync THREW: {ex.Message}");
    }
}

async Task LoadPhotoAsync(FileResult photo)
{
    // canceled
    if (photo == null)
    {
        PhotoPath = null;
        return;
    }
    // save the file into local storage
    var newFile = Path.Combine(FileSystem.CacheDirectory, photo.FileName);
    using (var stream = await photo.OpenReadAsync())
    using (var newStream = File.OpenWrite(newFile))
        await stream.CopyToAsync(newStream);

    PhotoPath = newFile;
}

I want to take the image and convert it into a byte[]

JoeCold
  • 47
  • 1
  • 8
  • 1
    You asked this earlier today and I linked to a duplicate question. The object returned by MediaPicker contains a stream, which can easily be converted to a byte[]. There are numerous existing questions about converting a C# stream to a byte[] – Jason Nov 17 '20 at 16:08
  • you can also use `File.ReadAllBytes` to read a file as a `byte[]` – Jason Nov 17 '20 at 16:35
  • @Jason sorry i believe stack removed the post for lack of detail. I will have a look into it if you have a sample that would be appreciated. – JoeCold Nov 17 '20 at 18:41
  • see the linked question – Jason Nov 17 '20 at 18:51

0 Answers0