Resize the image before Uploading to Api

Vuyiswa Maseko 351 Reputation points
2021-12-06T21:57:44.863+00:00

Good Day

My users tend to upload photos that are around 3mb or 5mb which takes a long time to render on the app. i would like to limit the images.

1) Is there a generic function or NuGet that can adjust the image and never loose its ratio or quality

2) What is a reasonable size that i can use to block the user if the size is exceed when uploading to an api

Please note i am using Xamarin and this reports an error in App centre

java.lang.RuntimeException: Canvas: trying to draw too large(106858752bytes) bitmap

Which obviously mean the image is too big to be rendered.

Thanks

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,291 questions
0 comments No comments
{count} votes

Accepted answer
  1. JarvanZhang 23,936 Reputation points
    2021-12-07T03:53:23.9+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    To resize the image before uploading, try using Bitmap.Compress method to compress the image. Get the compressed stream or byte array and update it to the server.

    Here is the sample code, you cuold refer to it.

       Bitmap bitmap = BitmapFactory.DecodeFile("fileName");//or BitmapFactory.DecodeStream  
       var outStream = new System.IO.MemoryStream();  
       bitmap.Compress(Bitmap.CompressFormat.Png, quality, outStream);  
         
       //get the compressed byte array  
       var outArray = outStream.ToArray();  
    

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Alessandro Caliaro 4,181 Reputation points
    2021-12-07T06:52:17.137+00:00

    You can use FFImageLoading library

    4399386