Hi all,
A quick question on WinForms:
I'm trying to delete images using the Microsoft.VisualBasic.dll file. It works fine in itself.
However, I'm trying to dispose a thumbnail image from a List and for some reason, I get error messages saying that the file is in use. This doesn't happen all the time, which means it's very difficult for me to pinpoint the problem.
The thumbnail images are being used in a datagridviews datarows, and I think I dispose an image alright, but somethng is obviously wrong.
Here is the code (constructor, creating a thumbnail image, and deleting a thumbnail image:
public Pic(int id, string name, byte grade, string path, Image thumb, bool selected )
{
this.id = id;
this.name = name;
this.grade = grade;
this.path = path;
this.thumb = thumb;
this.selected = selected;
}
private void bLoadImages_Click(object sender, EventArgs e)
{
if (ofdImages.ShowDialog() != DialogResult.Cancel)
{
int i = 0;
plh.clearList();
dgv.DataSource = null;
dgv.Rows.Clear();
foreach (string picname in ofdImages.FileNames)
{
Image img = Image.FromFile(picname);
Image thumby = thumbnail2(img, img.Width, img.Height);
Pic pic = new Pic(i, picname, 0, "", thumby, false);
plh.addToList(pic);
img.Dispose();
i++;
}
updateDGV();
}
}
...
Image thumb = image.GetThumbnailImage(Convert.ToInt32(dWidth), Convert.ToInt32(dHeight), () => false, IntPtr.Zero);
return thumb;
if (imgpath == PicList.imgList[j].name)
{
PicList.imgList[j].thumb.Dispose();
PicList.imgList.RemoveAt(j);
try
{
Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile(imgpath, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin, UICancelOption.DoNothing);
}
Does someone see what's wrong? If not, is there any way to see what control or parameter is keeping the image alive? (I only see the error from the DeleteFile dialog box, which simply states that the image is in use.
Thanks a lot!
Petter