Link to home
Create AccountLog in
Avatar of MSAIT
MSAIT

asked on

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

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;
                }
        }
ASKER CERTIFIED SOLUTION
Avatar of Misbah
Misbah
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.