1
private void button4_Click(object sender, EventArgs e)
{
        OCR.recognize("test1.tif");
        System.IO.File.Delete("test1.tif"); // <--- Problem on this line
}

....

public static string recognize(string filepath, MODI.MiLANGUAGES language =
MODI.MiLANGUAGES.miLANG_RUSSIAN)
{
        if (!File.Exists(filepath)) return "error 1: File does not exist";
        MODI.Document doc = new MODI.Document();
        doc.Create(filepath);

        doc.OCR(language, true, true);
        MODI.Image image = (MODI.Image)doc.Images[0];

        string result="";
        foreach (MODI.Word worditems in image.Layout.Words)
        {
            result += worditems.Text + ' ';

        // Processed image is ALWAYS a question
            if (worditems.Text[worditems.Text.Length - 1] == '?') break;
        }


        doc.Close();

        return result;

}

Problem is: File is used by another process.

How do I delete it after OCR?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
John black
  • 79
  • 3

1 Answers1

0

Someone posted a solution for this:

public void Dispose()  
{   
        doc.Close(false); 
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(doc);
        doc = null; 
        GC.Collect();
}

Maybe it works for your case, too.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • You can still use Process Monitor from SysInternals to see which process actually accesses the file. Maybe it is something different than you currently think is it. http://technet.microsoft.com/en-us/sysinternals/bb896645 – Uwe Keim Dec 31 '10 at 11:27
  • No, its that application. quiz.vshost.exe where quiz is my app. – John black Dec 31 '10 at 11:46
  • 1
    This fixed it: image = null; GC.Collect(); GC.WaitForPendingFinalizers(); – John black Dec 31 '10 at 12:31