Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:


C#
//Constructor
        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;
        }




C#
        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();
            }
        }


...

//To create the thumbnail image
            Image thumb = image.GetThumbnailImage(Convert.ToInt32(dWidth), Convert.ToInt32(dHeight), () => false, IntPtr.Zero);
            return thumb;



C#
if (imgpath == PicList.imgList[j].name)
                        {
                            PicList.imgList[j].thumb.Dispose(); //delete the thumbnail image
                            PicList.imgList.RemoveAt(j); //remove file instance from list of images
                            try //move file to recycle bin (for this, reference is needed to microsoft.VisualBasic.dll)
                            {
                                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
Posted

1 solution

Hi I think you are trying to delete the picture after disposing it but for some reason it's taking time to dispose the picture.
Try removing the picture after Disposed Handler and if there is not any Disposed method then you can use a while loop (in the try...catch section).
 
Share this answer
 
v3
Comments
petter2012 31-Mar-13 15:55pm    
Hi,

Thanks a lot for your reply!

If I understand you correctly, you mean that .Net doesn't dispose of the image just because I told it to. (The Dispose method is .Net's own, not a method of mine.)

So do you mean that I should do a while loop, kind of:

<pre lang="c#">while !(disposed)
{
Thread.Sleep(500);
}</pre>

This sounds a bit spooky to me, but perhaps I read you wrong.

Thanks again,
Petter
Shahin Khorshidnia 31-Mar-13 17:50pm    
Hello,
I am not sure that Dispose method is a real .NET method. I think it has been implemented in the VB DLL, however, I think the disposing takes a little time that's why told you try to Delete file in a Disposed Handler method or put it in a loop.
petter2012 31-Mar-13 18:45pm    
Ah - okey I see. Then I understood you correctly after all. Thanks! :)
Shahin Khorshidnia 31-Mar-13 21:46pm    
Youre welcome Petter :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900