2015-10-07 153 views
0

enter image description here Foreach img在一個文件夾中繪製一個圖片框在面板上,當我嘗試重新繪製pictureboxes(我刪除之後)'panel.controls.clear();'行給出一個錯誤:Panel.controls.Clear無效的參數錯誤

Blockquote An unhandled exception of type 'System.ArgumentException' occurred in System.Drawing.dll Additional information: invalid parameter.

private void removeScreenshot_Click(object sender, EventArgs e) 
    { 
     Button btn = sender as Button; 
     PictureBox pb = btn.Parent as PictureBox; 
     string imgString = pb.Tag.ToString() 
     pb.BackgroundImage.Dispose(); 
     pb.Image.Dispose(); 
     try 
     { 
      File.Delete(imgString); 
      pb.Dispose(); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine("Cannot delete img: " + ex); 
     } 
     reload(); 
    } 

下面重載()函數:

using (var bmpTemp = new Bitmap(imgString)) 
{ 
    src = new Bitmap(bmpTemp); 
} 

作爲替換:

private void reload() 
    { 
     bool firstImg = true; 
     string[] fileList = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\screenshots"); 
     List<string> listOfStrings = new List<string>(fileList); 
     string supportedExtensions = "*.jpg,*.gif,*.png,*.bmp,*.jpe,*.jpeg"; 

     listOfStrings.Reverse(); 

     screenShotPanel.Controls.Clear(); 
     if (listOfStrings.Count > 0) 
     { 
      foreach (string imgString in listOfStrings) 
      { 
       string extension = System.IO.Path.GetExtension(imgString); 
       if (supportedExtensions.Contains(extension)) 
       { 
        PictureBox pb = new PictureBox(); 
        pb.Click += new EventHandler(click_pb); 
        pb.MouseHover += new EventHandler(mouseHover_pb); 
        pb.MouseLeave += new EventHandler(mouseLeave_pb); 
        pb.Height = 100; 
        pb.Width = 100; 
        pb.Location = new Point(x, y); 

        Bitmap src = Image.FromFile(imgString) as Bitmap; 
        Bitmap cropped = CropBitmap(src, pb.Width, pb.Height); 

        Button removeScreenshot = new Button(); 
        removeScreenshot.Height = 20; 
        removeScreenshot.Width = 20; 
        removeScreenshot.Location = new Point(80, 0); 
        removeScreenshot.BackColor = Color.Transparent; 
        removeScreenshot.ForeColor = Color.Transparent; 
        removeScreenshot.FlatStyle = FlatStyle.Flat; 
        removeScreenshot.FlatAppearance.BorderSize = 0; 
        removeScreenshot.MouseHover += new EventHandler(mouseHover_removeButton); 
        removeScreenshot.MouseLeave += new EventHandler(mouseLeave_removeButton); 
        removeScreenshot.Click += new EventHandler(removeScreenshot_Click); 
        pb.Controls.Add(removeScreenshot); 
        pb.BackgroundImage = src; 
        if (firstImg) 
        { 
         pictureBox.Image = src; 
         firstImg = false; 
        } 
        pb.Image = cropped; 
        pb.Tag = imgString; 
        pb.Name = Path.GetFileName(imgString); 
        screenShotPanel.Controls.Add(pb); 
        x = x + 120; 
       } 

      } 
     } 
     else 
     { 
      pictureBox.Image = null; 
      pictureBox.BackgroundImage = null; 
     } 
    } 
+0

不知道是什麼原因造成的,但是Clear()不處理這些控件。嘗試'while(screenShotPanel.Controls.Count> 0)screenShotPanel.Controls [0] .Dispose()'代替。可能需要更多地記錄添加到該面板的控件。 – LarsTech

+0

Wile循環似乎不起作用,同樣的問題沒有錯誤信息。添加了添加圖片框的代碼.. –

+0

仍然不確定問題出在哪裏,但是'Image.FromFile'會對文件保持鎖定狀態,所以這可能是需要看的東西。我無法複製錯誤。 – LarsTech

回答

-1

的問題是由以下代碼固定的溶液此代碼:

Bitmap src = Image.FromFile(imgString) as Bitmap;