resize or compress capture image

Bhuwan 881 Reputation points
2023-04-27T11:56:09.02+00:00

How to compress or resize capture image in Maui

for image capture I am using below code

 if (MediaPicker.Default.IsCaptureSupported)
            {
                FileResult photo = await MediaPicker.Default.CapturePhotoAsync();
                if (photo == null)
                    return;
                byte[] imageData;
                string localFilePath = Path.Combine(FileSystem.CacheDirectory, photo.FileName);
                using Stream stream = await photo.OpenReadAsync();
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    stream.CopyTo(memoryStream);
                    imageData = memoryStream.ToArray();
                    memoryStream.Dispose();
                }
}
Developer technologies | .NET | .NET MAUI
0 comments No comments
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,126 Reputation points Microsoft External Staff
    2023-04-28T02:09:59.81+00:00

    Hello,

    You could refer to this Xamarin official Image resizer example: ImageResizer.

    After my tests, this code works fine in MAUI Android and iOS. For Windows, you need to replace WINDOWS_UWP with WINDOWS.

    Then, you could call this method in your code in the following ways:

    imageData = memoryStream.ToArray();
    var resizedImgData = await ImageResizer.ResizeImage(imageData, 300, 300); 
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, 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.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.