How to compress images in Xamarin.Forms?

Asked

Viewed 225 times

1

I am developing an application in Xamarin.Forms that uses the camera of the mobile phone to capture images, however, depending on the client’s mobile the image can reach up to 12MB, this is too much for our server.

During my searches, I discovered that the class Bitmap of System.Drawing works only with Windows and not with Xamarin, so I eliminated this possibility.

In some places I discovered I could use the Bitmap of Xamarin, so I did the following:

using (var ms = new MemoryStream())
{
    stream.CopyTo(ms);
    ImageBytes = ms.ToArray();

    Bitmap imagemBitmap = BitmapFactory.DecodeByteArray(ImageBytes, 0, ImageBytes.Length);
    imagemBitmap.Compress(Bitmap.CompressFormat.Jpeg, 5, ms);

    bytesCompactados = ms.ToArray();
}

However, unfortunately, regardless of the quality I put among the parentheses of Compress, be it 0 or 100, it does not interfere in anything and ends up increasing the size of the image to decrease.

To decrease the size, you can change the resolution and/or compress the image quality.

Does anyone know how to fix this, or maybe they have an idea of a better way to do it? Thanks in advance

1 answer

2


If you use Crossmedia to take the photos you can configure image compression directly from it, follow an example of code:

var file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions()
            {

                CompressionQuality = 40 // vai de 0 a 100, quanto menor for menor a qualidade, consequentemente menor o tamanho também.

            });
  • It worked, thank you very much!!

  • @Renanmussatto Good bro, do not forget to mark as accepted answer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.