小程序优化
直接从新闻组贴过来的,见谅。
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.
------解决方案--------------------这英文难道是中国人写的,我程度那么差居然都看懂了,代码写的也很清晰...
不过就是不知道~~
------解决方案--------------------顶下
------解决方案--------------------