MAUI. I have read (from a database) a byte array named "receiptDocumentBytes".
I want to create a thumbnail of this image, and only pass that thumbnail among my various objects in the application (the images themselves would be quite large).
So once I have my receiptDocumentBytes byteArray I have tried to do this:
if (receiptDocumentBytes != null)
{
Microsoft.Maui.Graphics.IImage image;
using (MemoryStream ms = new MemoryStream(receiptDocumentBytes))
#if ANDROID
image = Microsoft.Maui.Graphics.Platform.PlatformImage.FromStream(ms);
#else
image = Microsoft.Maui.Graphics.Win2D.W2DImage.FromStream(stream);
#endif
//Create the thumbnail of this image:
using (Stream stream = new MemoryStream(receiptDocumentBytes))
{
if (image != null)
{
int thumbnailSize = Preferences.Get("ThumbnailSize", 600);
//The largest dimension will be set to 'thumbnailSize' pixels while maintaining the aspect ratio:
IImage thumbnailImage = image.Downsize(thumbnailSize, true);
using (MemoryStream memStream = new())
{
thumbnailImage.Save(memStream);
}
}
}
}
How does this behave ? Oddly.
My MemoryStream ms (the first using) is populated (it has a length, a capacity, etc), but the image object
image = Microsoft.Maui.Graphics.Platform.PlatformImage.FromStream(ms);
is not - it shows Height and Width = System.NullReferenceException: Object reference not set to an instance of an object, all the other properties being null.
However, the object itself is not null, thus passing the test
if (image != null)
Then, on the line
IImage thumbnailImage = image.Downsize(thumbnailSize, true);
the execution does not show any exception, but jumps to the end of my
using (Stream stream = new MemoryStream(receiptDocumentBytes))
So the code does not execute, is not caught by my error handling, and it does not create any thumbnail image.
So.... what am I doing wrong here, please ?
Thank you.