Compress image while uploading

Vasanthakumar M 251 Reputation points
2021-04-27T09:08:15.247+00:00

Hi Xperts,

How to compress the image while uploading the xamarin forms.

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

1 answer

Sort by: Most helpful
  1. JarvanZhang 23,851 Reputation points
    2021-04-27T11:40:43.307+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    How to compress the image while uploading the xamarin forms.

    Try to create an 'ImageResizer' control class to resize the image on each platform. Check the code:

       public static class ImageResizer  
       {  
           static ImageResizer()  
           {  
           }  
         
           public static async Task<byte[]> ResizeImage(byte[] imageData, float width, float height)  
           {  
               var platform = Device.RuntimePlatform;  
               switch (platform)  
               {  
                   case Device.Android:  
                       return ResizeImageAndroid(imageData, width, height);  
                   case Device.iOS:  
                       return ResizeImageIOS(imageData, width, height);  
                   default:  
                       break;  
               }  
         
               return null;  
           }  
           public static byte[] ResizeImageAndroid(byte[] imageData, float width, float height)  
           {  
               // Load the bitmap  
               Bitmap originalImage = BitmapFactory.DecodeByteArray(imageData, 0, imageData.Length);  
               Bitmap resizedImage = Bitmap.CreateScaledBitmap(originalImage, (int)width, (int)height, false);  
         
               using (MemoryStream ms = new MemoryStream())  
               {  
                   resizedImage.Compress(Bitmap.CompressFormat.Jpeg, 100, ms);  
                   return ms.ToArray();  
               }  
           }  
           public static byte[] ResizeImageIOS(byte[] imageData, float width, float height)  
           {  
               UIImage originalImage = ImageFromByteArray(imageData);  
               UIImageOrientation orientation = originalImage.Orientation;  
         
               //create a 24bit RGB image  
               using (CoreGraphics.CGBitmapContext context = new CoreGraphics.CGBitmapContext(IntPtr.Zero,  
                                                    (int)width, (int)height, 8,  
                                                    4 * (int)width, CoreGraphics.CGColorSpace.CreateDeviceRGB(),  
                                                    CoreGraphics.CGImageAlphaInfo.PremultipliedFirst))  
               {  
         
                   RectangleF imageRect = new RectangleF(0, 0, width, height);  
         
                   // draw the image  
                   context.DrawImage(imageRect, originalImage.CGImage);  
         
                   UIKit.UIImage resizedImage = UIKit.UIImage.FromImage(context.ToImage(), 0, orientation);  
         
                   // save the image as a jpeg  
                   return resizedImage.AsJPEG().ToArray();  
               }  
           }  
         
           public static UIKit.UIImage ImageFromByteArray(byte[] data)  
           {  
               if (data == null)  
               {  
                   return null;  
               }  
         
               UIKit.UIImage image;  
               try  
               {  
                   image = new UIKit.UIImage(Foundation.NSData.FromArray(data));  
               }  
               catch (Exception e)  
               {  
                   Console.WriteLine("Image load failed: " + e.Message);  
                   return null;  
               }  
               return image;  
           }  
       }  
    

    Here is the related sample: https://learn.microsoft.com/en-us/samples/xamarin/xamarin-forms-samples/xamformsimageresize/

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    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.