1

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);
      }
James Z
  • 12,209
  • 10
  • 24
  • 44
Arup
  • 155
  • 11
  • Firstly, you can try to get the path of the image using `Path.Combine` and then use the `var b = File.ReadAllBytes(the_path_of_the_image)` and finally use `Convert.ToBase64String(b)` to convert it. – Alexandar May - MSFT Nov 07 '22 at 08:22
  • That works to get the base64 string, but image won't be resized. It will retain the original size, for me it was about 5 Mb. I want to avoid this and want to resize the image to around 1Mb of space or less. – Arup Nov 07 '22 at 10:17
  • You can use the resized the image: newImage before Converting it. – Alexandar May - MSFT Nov 08 '22 at 05:32
  • Resize not working for me. I don't know what might be the cause, but I am able to get the job done using .DownSize function. – Arup Nov 15 '22 at 06:47
  • Good job! You can post your detailed solution as this would help others who have similar issue. – Alexandar May - MSFT Nov 15 '22 at 08:21

1 Answers1

1

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);
                }
            }
Arup
  • 155
  • 11