XamarinEssential.MediaPicker Image Size

Bob Titular 1 Reputation point
2021-01-14T23:32:39.043+00:00

The current XamarinEssentials.MediaPicker api doesn't have an option to control the size/resolution of a image being saved by the camera. The older Xamarin Media Plugin library had this option. What's the current work around to adjust the size/resolution of a saved image?

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

1 answer

Sort by: Most helpful
  1. JarvanZhang 23,951 Reputation points
    2021-01-15T02:55:28.55+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    The current XamarinEssentials.MediaPicker api doesn't have an option to control the size/resolution of a image being saved by the camera

    The MediaPickerOptions class doesn't provide the size function like the PickMediaOptions class of the media plugin. You could report a feature request to the product team on github.

    What's the current work around to adjust the size/resolution of a saved image

    To resize teh image file, a workaround is to get the stream from the picker image and then resize it on each platform. Check the code:

       private async void Button_Clicked_1(object sender, EventArgs e)  
       {  
           var photo = await MediaPicker.CapturePhotoAsync();  
           var stream = photo.OpenReadAsync().Result;  
         
           ResizeImage(stream);  
       }  
       protected async void ResizeImage(Stream stream)  
       {  
           byte[] imageData;  
         
           using (MemoryStream ms = new MemoryStream())  
           {  
               stream.CopyTo(ms);  
               imageData = ms.ToArray();  
           }  
         
           byte[] resizedImage = await ImageResizer.ResizeImage(imageData, 100, 100);  
         
           image.Source = ImageSource.FromStream(() => new MemoryStream(resizedImage));  
       }  
       ...  
         
       public static class ImageResizer  
       {  
           static ImageResizer()  
           {  
           }  
           public static async Task<byte[]> ResizeImage(byte[] imageData, float width, float height)  
           {  
               return ResizeImageAndroid(imageData, width, height);  
           }  
           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();  
               }  
           }  
       }  
    

    For the function code other platforms, you could refer to this link:
    https://github.com/xamarin/xamarin-forms-samples/blob/master/XamFormsImageResize/XamFormsImageResize/ImageResizer.cs

    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.

    1 person found this answer helpful.