Dear all,
I made a c# program for my friend's wedding. It slides all photos in
specified folder automatically. To roll the pictures smoothly I firstly scan
and store all pictures in RAM and push one by one to PictureBox control and
display it when the Timer control ticks. Coding like this:
------------------------------------------------------
IList<Imagephotoes = new List<Image>();
LoadPhotoes(PhotoSlide.Properties.Settings.Default .PhotoFolder, ref
photoes);
private void LoadPhotoes(string folderPath, ref IList<Imagephotoes)
{
DirectoryInfo currentDir = new DirectoryInfo(folderPath);
if (currentDir.Exists)
{
DirectoryInfo[] subDirs = currentDir.GetDirectories();
foreach (DirectoryInfo dir in subDirs)
{
LoadPhotoes(dir.FullName, ref photoes);
}
FileInfo[] files = currentDir.GetFiles();
foreach (FileInfo file in files)
{
string fileExt = file.Extension.ToUpper();
if (fileExt == ".JPG" || fileExt == ".JPEG")
{
using (FileStream stream = file.OpenRead())
{
photoes.Add(Image.FromStream(stream));
}
}
}
}
}
.... ...
this.pictureBox1.Image = GetSlidePhoto(photoes, lastPhotoIndex);
----------------------------------
The code can work, but it sucks the memory very much. As you see, this line
of code -
/* photoes.Add(Image.FromStream(stream)); */ will stack all Image objects
in RAM. In my test, I slide about 50 pictures which are total about 50 Mb( ~
1 Mb for 1 picture), it costs 300Mb RAM to run it.
Do you have any idea to improve this small program? Thanks.
--
Sincerely,
Mike Chen
http://chagel.com