ImageGear .NET v24.14 - Updated
Tutorial: Create Your First OCR Project for a Console Application
User Guide > How to Work with... > OCR > Getting Started with ImageGear OCR > Tutorial: Create Your First OCR Project for a Console Application

In this tutorial, you will configure a C# or VB.NET project for a console application and use ImageGear .NET OCR capabilities to load an image, OCR and export the data to the console.

For local development, ImageGear .NET must be installed from the product installer. This will ensure that licensing is set up properly for local development. Once ImageGear .NET is installed, projects can use NuGet for building and deployment.

The following tutorial refers specifically to 64-bit installations; for 32-bit installations:

Using the desired version of Visual Studio (2010 or later):

  1. Create a new "Console App (.NET Framework)" project, using C# or VB.NET, and name the project: my_first_OCR_project
  2. If you installed ImageGear .NET 64-bit, using the Configuration Manager, create a new project platform (if you don't have one already) for x64. Make sure your project is set to compile targeting Debug and x64. Confirm you now have $YOURLOCALPROJ\bin\x64\Debug\, and if it's not there, create it.
  3. Add references and required resources into your projects in one of the following ways:
    • Manually:
    • Copy all files (and folders) inside $INSTALLDIR\ImageGear .NET v24 64-bit\Bin\x64 to your local output bin directory in your project (i.e., $YOURLOCALPROJ\bin\x64\Debug\ ).
    • Add the following references to your project from $YOURLOCALPROJ\bin\x64\Debug\:
    • ImageGear24.Core.dll
    • ImageGear24.Evaluation.dll
    • ImageGear24.Formats.Common.dll
    • ImageGear24.Formats.SimplifiedMetadata.dll
    • ImageGear24.Recognition.dll
    • ImageGear24.Recognition.Forms.dll
Your output target directory (output path) should be set to $YOURLOCALPROJ\bin\x64\Debug\ 
  1. At this point all necessary assembly references and resources have been added to the project. 
    Add the following using statements to the top of the file.
    C#
    Copy Code
    using System;
    using System.IO;
    using ImageGear.Core;
    using ImageGear.Formats;
    using ImageGear.Recognition;
    using ImageGear.Evaluation;
    VB.NET
    Copy Code
    Imports ImageGear.Core
    Imports ImageGear.Formats
    Imports ImageGear.Recognition
    Imports ImageGear.Evaluation
    Imports System.IO
  1. Add the following code to create a private method to OCR, and export the data to the console. This code sets up licensing, initializes the ImageGear Formats assembly and initializes the Recognition engine.
    C#
    Copy Code
    namespace my_first_OCR_project
    {
       class Program
       {
         public void OCRFile(string fileToBeRecognizedPath)
         {
           // Initialize the license.
           ImGearEvaluationManager.Initialize();
           ImGearEvaluationManager.Mode = ImGearEvaluationMode.Watermark;
           // Initialize the common formats.
           ImGearCommonFormats.Initialize();
           // Initialize the Recognition object.
           ImGearRecognition igRecognition = new ImGearRecognition();
           ImGearPage igPage;
    VB.NET
    Copy Code
    Module Module1
      Public Sub OCRFile(fileToOCR As String)
        ' Initialize the license.
        ImGearEvaluationManager.Initialize()
        ImGearEvaluationManager.Mode = ImGearEvaluationMode.Watermark
        ' Initialize the common formats.
        ImGearCommonFormats.Initialize()
        ' Initialize the Recognition object.
        Dim igRecognition As New ImGearRecognition()
        Dim igPage As ImGearPage
  1. Add the following code to load the image, and set the ImGearRecPage.
    C#
    Copy Code
    using (FileStream localFile = new FileStream(fileToBeRecognizedPath, FileMode.Open, FileAccess.Read))
       igPage = ImGearFileFormats.LoadPage(localFile, 0);
    if (igPage is ImGearVectorPage)
    {
        // Rasterize vector images before performing OCR.
        igPage = (igPage as ImGearVectorPage).Rasterize(24, 300, 300);
    }
    ImGearRecPage igRecPage = igRecognition.ImportPage((ImGearRasterPage)igPage);
    VB.NET
    Copy Code
    Using localFile As New FileStream(fileToOCR, FileMode.Open, FileAccess.Read)
       igPage = ImGearFileFormats.LoadPage(localFile, 0)
    End Using
    If TypeOf igPage Is ImGearVectorPage Then
        ' Rasterize vector images before performing OCR.
        igPage = DirectCast(igPage, ImGearVectorPage).Rasterize(24, 300, 300)
    End If
    Dim igRecPage As ImGearRecPage = igRecognition.ImportPage(DirectCast(igPage, ImGearRasterPage))
  1. Add the following code to preprocess the image, and output the data to the console.
    C#
    Copy Code
    igRecPage.Image.Preprocess();
    igRecPage.Recognize();
    Console.Write(igRecPage.Text);
    VB.NET
    Copy Code
    igRecPage.Image.Preprocess()
    igRecPage.Recognize()
    Console.Write(igRecPage.Text)
  1. Add the following code to dispose the igRecognition and page object.
    C#
    Copy Code
    // You must explicitly call the page/document dispose method before calling igRecognition.Dispose()
       igRecPage.Dispose();
       igRecognition.Dispose();                            
    VB.NET
    Copy Code
    ' You must explicitly call the page/document dispose method before calling igRecognition.Dispose()
     igRecPage.Dispose()
     igRecognition.Dispose()                        
  1. Finally, add in the Main function, the OCRFile method passing in your image, and output to the console. Replace "C:\PATHTOOCR\FILENAME.tif" with the path to your own image.
    C#
    Copy Code
    static void Main(string[] args)
      {
         Program myProgram = new Program();
         myProgram.OCRFile(@"C:\PATHTOOCR\FILENAME.tif");
       }
    VB.NET
    Copy Code
        Sub Main()
            OCRFile("C:\PATHTOOCR\FILENAME.tif")
         End Sub
     End Module