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:
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);
}