C# - Can not access File which is already being used - Iron OCR

810 views Asked by At

I am using "Iron OCR", something like "Tesseract" to detect and scan certain Text from Screenshots.

Now I have the following error. Every time Iron OCR is used to scan an image for text it tries to access the Iron OCR log file which is somehow still used by the process before. So every time I get the error message that it can't access the log file because it is already in use. Nevertheless the Scan still works and I get a valid result even tho it gives me an exception because of that error.

My program works like this:

  1. it takes a screenshots of certain areas of my screen.
  2. it analyzes that image with Iron OCR and looks for text.
  3. this process repeats itself infinitely.

I have following code:

//------------------------- # Capture Screenshot of specific Area # -------------------------\\

Rectangle bounds3;
Rect rect3 = new Rect();
bounds3 = new Rectangle(rect3.Left + 198, rect3.Top + 36, rect3.Right + 75 - rect3.Left - 10, rect3.Bottom + 30 - rect3.Top - 10);
CursorPosition = new Point(Cursor.Position.X - rect.Left, Cursor.Position.Y - rect.Top);

Bitmap result3 = new Bitmap(40, 14);

using (Graphics g = Graphics.FromImage(result3))
{
   g.CopyFromScreen(new Point(bounds3.Left, bounds3.Top), Point.Empty, bounds3.Size);
}

//------------------------- # Analyze Image for Text # -------------------------\\

var Ocr = new IronTesseract();
using (var Input = new OcrInput(result))
{
   Input.Contrast();
   Input.EnhanceResolution(300);
   Input.Invert();
   Input.Sharpen();
   Input.ToGrayScale();
                   
   try 
   { 

 //------------------- # This causes the Error - Using Try Catch to Ignore it # -------------------\\
      var Result = Ocr.Read(Input);                                            
      text = Result.Text;                                                     
  
   }
   catch
   {
   }
             
}

Also removing all the above only using their "1 Line Code" gives the same error message:

var Result = new IronTesseract().Read(@"images\image.png").Text;

I hope someone can help me to figure out what exactly causes that issue.

0

There are 0 answers