How to convert ImageSource to Byte array?

If you are using Xamarin, you can use this:

public async Task<byte[]> ConvertImageSourceToBytesAsync(ImageSource imageSource)
{
    Stream stream = await ((StreamImageSource)imageSource).Stream(CancellationToken.None);
    byte[] bytesAvailable = new byte[stream.Length];
    stream.Read(bytesAvailable, 0, bytesAvailable.Length);

    return bytesAvailable;
}

Leave a Comment