C# Panel.controls.Clear无效参数错误

C# Panel.controls.Clear无效参数错误,c#,controls,picturebox,C#,Controls,Picturebox,对于文件夹中的每个img,我在面板上绘制一个picturebox,当我尝试重新绘制picturebox(在移除一个picturebox后)时,使用“panel.controls.clear();”第行给出了一个错误: Blockquote System.Drawing.dll中发生“System.ArgumentException”类型的未处理异常。其他信息:参数无效 下面是reload()函数: private void reload() { bool firstIm

对于文件夹中的每个img,我在面板上绘制一个picturebox,当我尝试重新绘制picturebox(在移除一个picturebox后)时,使用“panel.controls.clear();”第行给出了一个错误:

Blockquote System.Drawing.dll中发生“System.ArgumentException”类型的未处理异常。其他信息:参数无效

下面是reload()函数:

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;
        }
    }
private void reload()
{
bool firstImg=真;
string[]fileList=Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)+“\\screenshots”);
List listOfStrings=新列表(文件列表);
string supportedExtensions=“*.jpg,*.gif,*.png,*.bmp,*.jpe,*.jpeg”;
listOfStrings.Reverse();
screenShotPanel.Controls.Clear();
如果(listOfStrings.Count>0)
{
foreach(listofstring中的字符串imgString)
{
字符串扩展名=System.IO.Path.GetExtension(imgString);
if(supportedExtensions.Contains(扩展))
{
PictureBox pb=新PictureBox();
pb.Click+=neweventhandler(单击pb);
pb.MouseHover+=新事件处理程序(MouseHover_pb);
pb.MouseLeave+=新的事件处理程序(MouseLeave_pb);
pb.高度=100;
pb.宽度=100;
pb.位置=新点(x,y);
位图src=Image.FromFile(imgString)作为位图;
位图裁剪=CropBitmap(src,pb.宽度,pb.高度);
按钮移除Screenshot=新按钮();
移除筛管。高度=20;
移除筛管。宽度=20;
removeScreenshot.位置=新点(80,0);
removeScreenshot.BackColor=Color.Transparent;
removeScreenshot.ForeColor=Color.Transparent;
removeScreenshot.FlatStyle=FlatStyle.Flat;
removeScreenshot.FlatAppearance.BorderSize=0;
removeScreenshot.MouseHover+=新事件处理程序(MouseHover\u removeButton);
removeScreenshot.MouseLeave+=新事件处理程序(MouseLeave_removeButton);
removeScreenshot.Click+=新建事件处理程序(removeScreenshot\u Click);
pb.Controls.Add(删除Screenshot);
pb.BackgroundImage=src;
如果(第一个img)
{
pictureBox.Image=src;
firstImg=假;
}
pb.图像=裁剪;
pb.Tag=imgString;
pb.Name=Path.GetFileName(imgString);
screenShotPanel.Controls.Add(pb);
x=x+120;
}
}
}
其他的
{
pictureBox.Image=null;
pictureBox.BackgroundImage=null;
}
}

问题的解决方案由以下代码确定:

using (var bmpTemp = new Bitmap(imgString))
{
   src = new Bitmap(bmpTemp);
}
作为此代码的替换:

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

不确定是什么原因造成的,但
Clear()
不会处理控件。请尝试
while(screenShotPanel.Controls.Count>0)screenShotPanel.Controls[0]。Dispose()
。可能需要记录更多关于添加到该面板的控件的信息。Wile循环似乎不起作用,同样的问题没有错误消息。在添加picturebox的位置添加了代码。仍然不确定问题出在哪里,但Image.FromFile将对文件进行锁定,因此可能需要查看。我不能重复这个错误。
Bitmap src = Image.FromFile(imgString) as Bitmap;