Search code examples
androidxamarinxamarin.androidpicassoandroid-glide

Load resized bitmap into byte[] array


I have to send a downscaled bitmap to my database from Android (using Xamarin).

To do so, I read about Glide or Picasso. Glide seemed to be the best picture library so I've installed Glide-Xamarin 3.7.0.

But I don't get how to load an existing bitmap (from URI), downscale to specific[ish] resolution and save it into a byte[] array in order to send it with the Webservice I'm calling.

If anyone knows the recipe for that one, would be much appreciated.


Solution

  • Finally I did it with Picasso

    var bitmap = Picasso
                 .With(context)
                 .Load(originalBitmapUri)
                 .Resize(reqWidth, reqHeight)
                 .CenterInside()
                 .OnlyScaleDown()
                 .Get();
    

    This one-liner returns me a resized Bitmap, from which I can call CompressAsync(.)

    using (var stream = new MemoryStream())
    {
        await bitmap.CompressAsync(Bitmap.CompressFormat.Jpeg, quality, stream);
        return stream.ToArray();
    }