MODI OCR - Image being used by another process.

Associate
Joined
20 Jul 2009
Posts
41
I've posted this on stackoverflow but not sure if any one you guys will know.

I am using MODI to carry out some OCR tasks in C#.

In my main form I have a picture box which displays the image the user wishes to check for words. I have an OCR class which holds an update function for when the user wants to check the image (as seen below):

Code:
public void Update()
    {
        string result = "";
        Document doc = new Document();

        try
        {
            // Create a new document based on the file chosen by the user
            doc.Create(Global.fileName);

            // Update the progress (Event Handler)
            doc.OnOCRProgress += new _IDocumentEvents_OnOCRProgressEventHandler(this.ShowProgress);

            // Start the OCR process
            doc.OCR(MiLANGUAGES.miLANG_ENGLISH, true, true);

            // Retrieve all the words found in the image file and store them in a string.
            for (int i = 0; i < doc.Images.Count; i++)
            {
                if (i == doc.Images.Count - 1)
                    result += doc.Images[i].Layout.Text;
                else
                    result += doc.Images[i].Layout.Text;
            }
        }
        catch (Exception ex)
        {
            if (Global.fileName == "")
                Global.mainForm.UpdateOCRTextBox(Global.NoImageSelected);
            else
                Global.mainForm.UpdateOCRTextBox(ex.ToString());
        }

        finally
        {                
            doc.Close(false); // Close the document (without saving changes)
            doc = null;

            // Told to try below - did not work.
            //GC.Collect();   
            //GC.WaitForPendingFinalizers();
        }

        if (result != "")
            Global.mainForm.UpdateOCRTextBox(result); // Update the ocrTextBox with the writing found in the image.
    }

The problem I am having is with the releasing of the image. When the user carries out the OCR task and then tries to load the same image back into the picture box an exception is thrown. This exception is stating that the file is being used by another process (Update). I have tried making a copy of the image file, saving it and using that for the OCR tasks. When I come to delete the temp image I come across the exact same problem with the file being used. I can't see what I am doing wrong, I saw a suggestion to try adding:

Code:
GC.Collect();   
GC.WaitForPendingFinalizers();

This did not solve the problem. If anyone could shed light on the problem I would be very grateful!
 
I'm just off out, so only a quick couple of points.

Does the document class implement IDisposable? If so, use a Using statement to automatically call the dispose event upon finishing with the object.

I see no reference to disposing of the event handler prior to attempting to close and nullify the doc object. As the pointer (reference) to the event handler still exists on the Doc object, you'll probably struggle to dispose the object cleanly leaving a link to the image you're performing the OCR on. Remove the handler prior to doc.Close(false) and see how you get on.
 
I'm just off out, so only a quick couple of points.

Does the document class implement IDisposable? If so, use a Using statement to automatically call the dispose event upon finishing with the object.

I see no reference to disposing of the event handler prior to attempting to close and nullify the doc object. As the pointer (reference) to the event handler still exists on the Doc object, you'll probably struggle to dispose the object cleanly leaving a link to the image you're performing the OCR on. Remove the handler prior to doc.Close(false) and see how you get on.

Thanks very much for the suggestion.

No the document class does not implement IDisposable :(

I tried your suggestion of disposing the event handler too and that did not seem to solve the problem.
 
Sorry to bump this thread, just thought I would provide a bit of an update. The problem only seems to occur with images in the format of TIFF, Bitmaps are fine.

Is there anything extra I need to do to when releasing TIFFs?

Code:
 Images images = doc.Images;

for (int i = 0; i < doc.Images.Count; i++)
       doc.Images.Remove(images[i]);

images = null;

I tried adding the above to the finally block on the off chance it would actually do something... no luck there :( - Really annoying me now! :D
 
Long shot...
Remove then in reverse order (i.e. for(int I = count; I > 0; i--)).
Might be skipping over some images the way your doing it?
 
Sorry to bump this thread, just thought I would provide a bit of an update. The problem only seems to occur with images in the format of TIFF, Bitmaps are fine.

Is there anything extra I need to do to when releasing TIFFs?

Code:
 Images images = doc.Images;

for (int i = 0; i < doc.Images.Count; i++)
       doc.Images.Remove(images[i]);

images = null;

I tried adding the above to the finally block on the off chance it would actually do something... no luck there :( - Really annoying me now! :D

Can you post all your code, as this snippet doesn't exist in your original function?

Just downloading Sharepoint Designer as the MODI dll isn't provided in Office 2010. :(
 
Can you post all your code, as this snippet doesn't exist in your original function?

Just downloading Sharepoint Designer as the MODI dll isn't provided in Office 2010. :(

That's where I had to download it from also.

Don't worry about the other code snippet, I was just posting something alternative that I tried - I've deleted it now. Here is the code for where I load the image into a picture box:

Code:
 public void UpdatePictureBox()
        {
            Image image = null;
            FileStream fileStream = null;

            try
            {
                fileStream = new FileStream(Global.fileName, FileMode.Open, FileAccess.Read);
                image = new Bitmap(Image.FromStream(fileStream));

                if (image.Width > ocrPictureBox.Width || image.Height > ocrPictureBox.Height)  // Image needs to be resized 
                    this.ocrPictureBox.Image = ResizeImage(image, ocrPictureBox.Size);
                else
                    this.ocrPictureBox.Image = image;

                this.ocrText.Text = ""; // Reset the text
            }
            catch (Exception ex)
            {
                ErrorMessage errorMessage = new ErrorMessage(Global.ImageInUse);
                errorMessage.ShowDialog();
            }
            finally
            {
                image = null;

                if (fileStream != null)
                {
                    fileStream.Close();
                    fileStream.Dispose();
                }
            }
        }

this code is fine up until I run the OCR code which is displayed in the first post. When I try to reload the image which has just been OCRed it is caught here stating the image is currently in use. As I said before, this is only caught when using a tiff image file. Bitmaps cause no problems!
 
I think I found the solution! I misunderstood how the code was working. I thought this:

Code:
 for (int i = 0; i < doc.Images.Count; i++)
            {
                if (i == doc.Images.Count - 1)
                    result += doc.Images[i].Layout.Text;
                else
                    result += doc.Images[i].Layout.Text;
            }

was actually getting each word individually, hence why I was trying to add spaces to make it format correctly. Changing the above code to:

Code:
 for (int i = 0; i < doc.Images.Count; i++)
              result += doc.Images[i].Layout.Text;

solved my problem, just in case anyone else comes across something in the future. Also calling the garbage collector in the finally block is a necessity. When this was removed it began complaining again about the image being in use. Thanks to s-p for trying to help, appreciate it.
 
Back
Top Bottom