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

Avatar of undefined
Last Comment
icr

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Misbah

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
icr

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck