Skip to content

[Android] Unexpected high Bitmap.ByteCount when loading image via ImageSource.FromResource() or ImageSource.FromStream() in .NET MAUI #33239

@VishalOmprasad

Description

@VishalOmprasad

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

  1. Download and open the attached sample in the public repository.
  2. Run with the initial sample changes.
  3. Add breakpoint in the “OnButtonClicked” event handler.
  4. Click the “Byte Count” button.
  5. Debug and observe the Bitmap.ByteCount.
  6. Now, change logic to load the image as resource or stream and run the sample.
  7. Click the “Byte Count” button.
  8. 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

Relevant log output

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions