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.