I'm working on a personal project to get better with C#, and as part of that I have a zip file with 20 or so images saved inside. I want to load those images out of the zip file and display them in my WPF app. I have it working, but I don't know if what I have is the best way to go about this. The goal is to load the images to be used as fast as possible, so I tried using threads, which work, but again I'm not sure if this is the proper way to utilize them for this, or if threads are even the best way to achieve the quickest time for the task.
I would appreciate any feedback anyone could give to help me improve this code and write better code in the future.
In the code below, I have the two functions I use to open and get the images. the variable ChapterLocation is a string that stores the file location of the .zip file, and Pages is a List. Page is a custom struct that just holds a BitmapImage and a String that gives that image a name that I can display.
private void BitmapLoad(ZipArchiveEntry z)
{
using (var stream = z.Open())
{
using (var mstream = new MemoryStream())
{
stream.CopyTo(mstream);
mstream.Position = 0;
var bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.StreamSource = mstream;
bitmap.EndInit();
bitmap.Freeze();
Pages.Add(new Page(bitmap, z.Name));
}
}
}
private void LoadChapterOptimized()
{
var zipFile = ZipFile.OpenRead(ChapterLocation);
foreach (ZipArchiveEntry z in zipFile.Entries)
{
Thread t = new Thread(() => BitmapLoad(z));
t.Start();
t.Join();
Debug.WriteLine(z.Name);
}
Pages.Sort();
zipFile.Dispose();
}