Foreach img在一个文件夹中我在面板上绘制一个图片框,当我尝试重新绘制图片框(在我删除之后)' panel.controls.clear();&#39 ; line给出错误:
Blockquote类型' System.ArgumentException'的未处理异常发生在System.Drawing.dll中的附加信息:无效参数。
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();
}
在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;
}
}
答案 0 :(得分:-1)
问题的解决方案由以下代码修复:
using (var bmpTemp = new Bitmap(imgString))
{
src = new Bitmap(bmpTemp);
}
替换此代码:
Bitmap src = Image.FromFile(imgString) as Bitmap;