how to optimize memory on my photo slide program

M

Mike Chen

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<Image> photoes = new List<Image>();
LoadPhotoes(PhotoSlide.Properties.Settings.Default.PhotoFolder, ref
photoes);

private void LoadPhotoes(string folderPath, ref IList<Image> photoes)
{
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.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

| 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:

Instead of storing the photos store the path to the image, only when you
show a photo the photo is loaded in memory.
 
M

Mike Chen

Ignacio,
thanks for replying. I test that before my post. If we load the picture
before display it each time, the slide won't run smoothly (The time to new a
image from path flickers). I need to scroll every picture in 0.1 second.
 
M

Matthew Newman

Why not only load the next image and remove it from memory after its been
displayed. That way you only have the current image and the next image to be
displayed in memory.

-Matthew Newman
-http://www.bestsnowman.com/
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

| Ignacio,
| thanks for replying. I test that before my post. If we load the picture
| before display it each time, the slide won't run smoothly (The time to new
a
| image from path flickers). I need to scroll every picture in 0.1 second.

in 0.1 second ????


You can always load the next one.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top