2011-09-26 93 views
0

目前我正在使用ImageList控件,嘗試從網絡中複製文件並覆蓋ImageList上的內容。但是,當List填充後我嘗試複製圖像時,我無法加載圖像。我試過使用.Dispose()和.Images.Clear(),但是從我讀過的東西中除去了對圖像本身的引用,所以它可以被替換。圖片列表,刪除參考

 imageList1.Images.Clear(); 
     imageList2.Images.Clear(); 


     int i = 1500; 
     string fileName,sourcePath,targetPath,destFile = string.Empty; 
     Image img = null; 
     int counter = 0; 
     bool exists = false; 
     string image = string.Empty; 
     MiscManager MM = new MiscManager(); 
     //filePath = MM.GetBobbinImagePath(); 
     filePath = @"C:\Program Files\Images"; 
     sourcePath = @"\\network Images"; 
     targetPath = filePath; 

     if (!System.IO.Directory.Exists(targetPath)) 
     { 
      System.IO.Directory.CreateDirectory(targetPath); 
     } 

     if (System.IO.Directory.Exists(sourcePath)) 
     { 
      string[] files = System.IO.Directory.GetFiles(sourcePath); 

      foreach (string n in files) 
      { 
       fileName = System.IO.Path.GetFileName(n); 
       destFile = System.IO.Path.Combine(targetPath, fileName); 
       try 
       { 
        System.IO.File.Copy(n, destFile, true); 
       } 
       catch 
       { 
        MessageBox.Show("File in use",fileName); 
       } 

      } 
     } 

     do 
     { 
      if (i < 10) 
      { 
       fileName = "000" + Convert.ToString(i); 
      } 
      else if (i > 10 && i < 100) 
      { 
       fileName = "00" + Convert.ToString(i); 
      } 
      else if (i >= 100 && i < 1000) 
      { 
       fileName = "0" + Convert.ToString(i); 
      } 
      else 
      { 
       fileName = Convert.ToString(i); 
      } 

      image = filePath + fileName + ".bmp"; 
      exists = File.Exists(image); 

      if (exists) 
      { 
       img = Image.FromFile(image); 

       imageList1.Images.Add(img); 
       imageList2.Images.Add(img); 

       imageList1.Images.SetKeyName(counter, Convert.ToString(i) + ".bmp"); 
       imageList2.Images.SetKeyName(counter, Convert.ToString(i) + ".bmp"); 
       counter++; 
      } 


      i++; 
     } while (i < 10000); 

我對圖片列表瞭解不多,所以任何協助總是不勝感激。在C#和VS這樣做2010

回答

1

您可以使用MemoryStream讓你的圖像不扶住參考文件流:

using(MemoryStream ms = new MemoryStream(File.ReadAllBytes(image)) 
{ 
    img = Image.FromStream(ms); 
} 
+0

真棒,我不能嘗試的時刻,但我會在早上嘗試第一件事 –