Not able to Resize an IImage in .NET MaUI, "System.ObjectDisposedException: Cannot access a disposed object. Object name: Android.Graphics.Bitmap"

1.3k views Asked by At

Purpose: I am capturing an image with camera using MediaPicker, and then trying to resize the image to reduce it's size. Then converting this resized image to base64 string and storing it in mongodb finally. Issue: While converting to base64 using the function IImage.AsBase64(ImageFormat.Png, 1), it's throwing exception

System.ObjectDisposedException: Cannot access a disposed object. Object name: Android.Graphics.Bitmap

I am in need of a little guidance.

I tried saving the image as well as IImage.Save(sourceStream, ImageFormat.Png, 1) function saves the IImage to a stream as well and my idea was to use this stream to convert to base64. But this idea is also throwing the same mentioned exception. I tried to set the bool flag to false to set the dispose of the source image to false. But that also didn't work.

A snippet of the code:

enter image description here

  FileResult photo = await MediaPicker.Default.CapturePhotoAsync();
  localFilePath = String.Empty;

  if (photo != null)
  {
      // save the file into local storage
      localFilePath = System.IO.Path.Combine(FileSystem.CacheDirectory, photo.FileName);

      IImage image;
      using Stream sourceStream = await photo.OpenReadAsync();
      image = PlatformImage.FromStream(sourceStream);
      if (image != null)
      {
          Microsoft.Maui.Graphics.IImage newImage = image.Resize(500, 400,   ResizeMode.Fit, false);
          //newImage.Save(sourceStream, ImageFormat.Png, 1);
          base64ImageString = newImage.AsBase64(ImageFormat.Png, 1);
      }
1

There are 1 answers

0
Arup On

Below code shows how you can take/capture a photo and downsize it to another resolution. Here I have used 400 X 400(around 200kb). The original picture was of 1440×1080 (around 5mb). After downsizing it, I am converting the image to base64 for db storing purpose. (You can use base64 to image online converter to check back the downsized image again in any browser)

                using (Stream sourceStream = await photo.OpenReadAsync())
            {
                IImage image;
                image = PlatformImage.FromStream(sourceStream);
                if (image != null)
                {
                    Microsoft.Maui.Graphics.IImage newImage = image.Downsize(400, 400);
                    //newImage.Save(sourceStream, ImageFormat.Png, 1);
                    if (captureProductClicked)
                    {
                        base64ImageStringProduct = newImage.AsBase64(ImageFormat.Png, 1);
                        productImageAvlbl = true;
                    }
                    else if (captureCompititorClicked)
                    {
                        base64ImageStringCompetitor = newImage.AsBase64(ImageFormat.Png, 1); ;
                        competitorImageAvlbl = true;
                    }
                }
                using (FileStream localFileStream = File.OpenWrite(localFilePath))
                {
                    await sourceStream.CopyToAsync(localFileStream);
                }
            }