I am developing a project that needs to read information from .zip archives (name, description, image). For this task I took DotNetZip .

Made a DTO class:

 class ENBModel { public string Name { get; set; } public BitmapImage Image { get; set; } } 

Then I made a certain collection into which I load data from all found files, I do it like this:

 foreach (var file in files) { using (var zip = ZipFile.Read(file)) { var enb = new ENBModel(); using (var dataStream = new MemoryStream()) { var dataZip = zip["PSSData.json"]; dataZip.Extract(dataStream); var data = Encoding.Default.GetString(dataStream.ToArray()); var json = JsonConvert.DeserializeObject<FileDataModel>(data); enb.Name = json.Name; } var imageSource = new BitmapImage(); using (MemoryStream imageSteam = new MemoryStream()) { var imgZip = zip["Image.png"]; imgZip.Extract(imageSteam); imageSource.BeginInit(); imageSource.StreamSource = imageSteam; imageSource.CacheOption = BitmapCacheOption.OnLoad; imageSource.EndInit(); enb.Image = imageSource; } Presets.Add(enb); } } 

As a result, I have everything I need to display data in the View. I make it simple ListBox :

 <ListBox ItemsSource="{Binding Presets}" ScrollViewer.HorizontalScrollBarVisibility="Disabled"> <ListBox.ItemsPanel> <ItemsPanelTemplate> <WrapPanel/> </ItemsPanelTemplate> </ListBox.ItemsPanel> </ListBox> 

And after all these manipulations, I see that my application is not eating 60MB of RAM, but already all 500. I understand, yes, all images are being stored in memory, but how can I be without it?

Actually, help to optimize this matter. Wherever I find information about this, the advice is everywhere “Put CacheOption = BitmapCacheOption.OnLoad; ”, but as you can see, the result is zero. I tried other options, so I get gray images.

  • one
    Not in substance, but DTO should not contain complex classes like BitmapImage, otherwise it is not DTO - Andrey NOP
  • Now, in essence, the question: is it not completely clear what exactly you do not like? How much do the pictures weigh in the extracted state? How do you get them on a 100% scale? - Andrei NOP
  • Have you seen this: stackoverflow.com/a/698310/218063 ? - Andrei NOP
  • @ АндрейNOP A miniature is displayed on the screen that has a size of 140x140; when you hover the mouse, ToolTip is displayed with an image limited to 1000x1000. Weight images in the region of 1mb. Well, do not like the fact that so much eats memory. I do not think that there is a problem in the View part, because if I remove it completely, then the memory will also be clogged. The point is that I read all the pictures from the archives and fill up the collection with them, how else can I not be understood ... - EvgeniyZ
  • Ie you want to keep all the pictures in memory, but that they did not take anything there? - Andrey NOP

0