问题描述
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespace相册片头自动生成器{publicpartialclassForm1:Form{publicstaticinti=0;publicstring[]picArr=null;publicForm1(){CheckForIllegalCrossThreadCalls=false;InitializeComponent();}privatevoidOPENfile_Click(objectsender,EventArgse){ChooseFileDialog.ShowDialog();}privatevoidChooseFileDialog_FileOk(objectsender,CancelEventArgse){i=0;picArr=null;picArr=ChooseFileDialog.FileNames;listView1.Items.Clear();pic.Dispose();progressBar1.Maximum=picArr.Length;this.progressBar1.Value=0;backgroundWorkerOpenfile.RunWorkerAsync();}privatevoidbackgroundWorkerOpenfile_DoWork(objectsender,DoWorkEventArgse){openfile();}privatevoidbackgroundWorkerOpenfile_RunWorkerCompleted(objectsender,RunWorkerCompletedEventArgse){backgroundWorkerOpenfile.Dispose();}privatevoidForm1_FormClosed(objectsender,FormClosedEventArgse){Application.Exit();}privatevoidbackgroundWorkerOpenfile_ProgressChanged_1(objectsender,ProgressChangedEventArgse){this.progressBar1.Value++;}privatevoidopenfile(){if(i<picArr.Length){pic.Images.Add(Image.FromFile(picArr[i]));pic.ImageSize=newSize(144,80);listView1.LargeImageList=pic;ListViewItemlvi=newListViewItem();lvi.ImageIndex=i;this.listView1.Items.Add(picArr[i],i);this.progressBar1.Value++;GC.Collect();}else{return;}i++;openfile();}}}代码如上,打开24张图片之后,1G内存就没有了。