So i am filling a float layout panel with images, with this code:
private void FillPanel(string sql)
{
panel.Controls.Clear();
SQLiteConnection dbConnect = new SQLiteConnection("Data Source=" + dbFullPath + ";Version=3;");
dbConnect.Open();
SQLiteCommand runQuery = new SQLiteCommand(sql, dbConnect);
SQLiteDataReader reader = runQuery.ExecuteReader();
while (reader.Read())
{
PictureBox pB = new PictureBox();
pB.Image = Image.FromFile(reader["imgPath"].ToString());
pB.Size = new Size(100, 100);
pB.SizeMode = PictureBoxSizeMode.StretchImage;
pB.Padding = new Padding();
pB.Margin = new Padding(5,5,5,5);
pB.Name = reader["name"].ToString();
toolTip_Main.SetToolTip(pB, pB.Name);
pB.DoubleClick += img_DoubleClick;
panel.Controls.Add(pB);
}
dbConnect.Close();
}
And if i try to delete the source pictures later i get an errormessage.
"image.png is used by another process"
To delete the images i use following code:
private void Delete()
{
foreach (Control x in panel.Controls)
{
if (x is PictureBox)
{
PictureBox pb = x as PictureBox;
string name = pb.Name;
DirectoryInfo pF = new DirectoryInfo(pictureFolder);
foreach (FileInfo file in pF.GetFiles())
{
if(file.Name == name+".png")
{
pb.InitialImage = null;
pb.Dispose();
file.Delete();
break;
}
}
}
}
}
If i dont fill the panel with images, i am able to delete them. I just dont know what else i could do next to 'initialimage = null' & .dispose to getz rid of the images inside the panel.
But it seems that they are ghosting somewhere.
Any ideas on this?