C# Why application memory size increases when load the 10MB size image and converted the BitmapImage to stream using the JpegBitmapEncoder in WPF

ramya s 1 Reputation point
2021-04-28T11:20:57.077+00:00

Can anyone tell me how to solve this problem?

I have using the JpegBitmapEncoder to get the stream from the BitmapImage as like in the following code snippet.

                    JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                    MemoryStream stream = new MemoryStream();
                    encoder.Frames.Add(BitmapFrame.Create(bitmapImage));
                    encoder.Save(stream); 

But the application memory size is increase huge. I have loading the image with size as 10 MB.

When using the 10 MB size image to the Image control at load time:

Memory size: 230

When using the 10 MB size image to the Image control at load time and convert the BitmapImage to the stream using the JpegBitmapEncoder:

Memory size: 352

When using the 50 KB size image to the Image control:

memory size: 72

When using the 50 KB size image to the Image control at load time and convert the BitmapImage to the stream using the JpegBitmapEncoder:

Memory size: 80

Also tried to dispose the stream with a Using statement and dispose method also. But still the application memory not reduced.

Sample Link: https://github.com/bharathirajauk/MySamples/tree/master/ShapesLoading

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,191 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
7,020 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
560 questions
No comments
{count} votes

1 answer

Sort by: Most helpful
  1. DaisyTian-1203 11,506 Reputation points
    2021-04-29T03:07:07.123+00:00

    You can set DecodePixelWidth for large images which will save memory for project. If you use below code to load the picture

    <Image>  
                <Image.Source>  
                    <BitmapImage DecodePixelWidth="320" UriSource="Assets\image.jpg" />  
                </Image.Source>  
            </Image>  
    

    The Memory size is 24.8M, below is the result picture:
    92462-capture.png


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    No comments