-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[Android] Unexpected high Bitmap.ByteCount when loading image via ImageSource.FromResource() or ImageSource.FromStream() in .NET MAUI #33239
Description
Description
In a .NET MAUI project, on Android platform, observed a significant increase in Bitmap.ByteCount when loading an image (Embedded resource) using ImageSource.FromResource() or ImageSource.FromStream(). The memory footprint is much larger compared to loading the same image (MauiImage) directly from XAML.
When loading the image as an embedded resource or stream:
this.image.Source = ImageSource.FromResource("BitmapTest.Resources.Images.bmpimage.bmp");
// OR
Assembly assembly = Assembly.GetExecutingAssembly();
this.image.Source = ImageSource.FromStream(() => assembly.GetManifestResourceStream("BitmapTest.Resources.Images.bmpimage.bmp"));
Bitmap.ByteCount becomes ~25,256,000 bytes.
When loading the image directly as MauiImage:
<Image x:Name="image"
Source="bmpimage2.bmp" />
Bitmap.ByteCount remains ~1,090,560 bytes, which seems normal.
This discrepancy suggests that the image rendering pipeline for resource/stream-based loading might be causing excessive memory allocation.
Steps to Reproduce
- Download and open the attached sample in the public repository.
- Run with the initial sample changes.
- Add breakpoint in the “OnButtonClicked” event handler.
- Click the “Byte Count” button.
- Debug and observe the Bitmap.ByteCount.
- Now, change logic to load the image as resource or stream and run the sample.
- Click the “Byte Count” button.
- Debug and you will observe excessive increase in Bitmap.ByteCount.
Link to public reproduction project repository
https://github.com/VishalOmprasad/MAUI-Issues/tree/main/BitmapTest
Version with bug
Unknown/Other
Is this a regression from previous behavior?
Not sure, did not test other versions
Last version that worked well
Unknown/Other
Affected platforms
Android
Affected platform versions
No response
Did you find any workaround?
No response