question

ramyas-5368 avatar image
0 Votes"
ramyas-5368 asked DaisyTian-1203 answered

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

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

dotnet-csharpwindows-wpfdotnet-wpf-xaml
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

DaisyTian-1203 avatar image
0 Votes"
DaisyTian-1203 answered

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.


capture.png (336.4 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.