C# File in use problem 500 POINTS!!!!!

MSAIT
MSAIT used Ask the Experts™
on
I have the code below which works great. However I need to loop through images in a folder to convert them. In the same process after the below routene has run I use the tiff file in some document processing. Then I need to delete the tiff file automatically. Problem is it cannot delete the last file used as it throws "File in use by another process" exeption. How can I make sure that the tiff file is no longer used in the code below after the line  bm.Save() ?
       
private Boolean Load_Image(string SourcePath)
        {
            try
            {
                DateTime i = DateTime.Now;
                label1.Text = "Opening Image....";
                Application.DoEvents();
                System.Drawing.Image bm = System.Drawing.Image.FromFile(SourcePath);
                label1.Text = "Converting Image....";
                Application.DoEvents();

                bm.Save(SourcePath.Replace(".", "") + "_output.Tiff", System.Drawing.Imaging.ImageFormat.Tiff);
                bm.Dispose();
                bm = null;
               
                DateTime o = DateTime.Now;
                TimeSpan u = o - i;
                label1.Text = "Conversion Complete." + u.ToString();
                Application.DoEvents();
                return true;
            }
            catch(Exception e)
                {
                    label1.Text = "Input File Not ready!" + e.Message ;
                    return false;
                }
        }
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Sr. Software Engineer
Commented:
after you are done with the processing
add this line:
 bm.Dispose();


icr
Commented:
Alternatively you can wrap it in a using statement, which is really just a bit of syntactic sugar for "When you exit this code block, call the dispose method".
You would do it like this:

private Boolean Load_Image(string SourcePath)
        {
            try
            {
                // ....
                using (System.Drawing.Image bm = System.Drawing.Image.FromFile(SourcePath))
                {
                        // ....
                        Application.DoEvents();
                        return true;
                }
            }
            catch(Exception e)
                {
                    label1.Text = "Input File Not ready!" + e.Message ;
                    return false;
                }
        }
}

This has the advantage that should the code exit early, i.e. on an exception (and you really shouldn't be catching the base Exception if you are in your real code) it will still call the dispose method. If you meerly put a call to dispose at the end it will not be disposed should the code exit early.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial