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):
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:
This did not solve the problem. If anyone could shed light on the problem I would be very grateful!
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!