HOWTO · Csharp

How to Implement OCR in a C# Project

Learn how to add Tesseract or IronOCR to a C# project and recognize text in an image.

On this page

To recognize text in an image from a C# application, install an OCR library, provide an image and any required language data, then read the recognized text from the result. This guide outlines two separate options: the open-source Tesseract engine and the commercial IronOCR library. Their packages, setup, and APIs are different.

What OCR Does in a C# Application

Optical Character Recognition (OCR) converts text represented in an image into machine-readable text. A C# application can use the result to index scanned pages, prefill data-entry forms, or make image content searchable. Recognition quality depends on the source image and language configuration; no library can guarantee that every character will be identified correctly.

Install Tesseract and IronOCR

Choose one library for a project, then follow that library’s package and deployment requirements. In Visual Studio, open the project’s NuGet package manager and install the package named Tesseract for the standalone Tesseract route, or IronOcr for IronOCR. Confirm the package identity and version against the Tesseract NuGet listing or the IronOcr NuGet listing before adding it; package APIs and native dependencies can vary by version and target platform.

Tesseract also needs trained language data. Obtain the .traineddata file for each language you intend to recognize and place the files together in a tessdata directory that your application can read. Include this directory in deployment, and use its actual path when creating the engine. The Tesseract project documentation describes the .NET wrapper and its setup. IronOCR is a separate library: do not pass its types to the standalone Tesseract package or assume the two packages share setup steps. Check the selected IronOCR version’s official C# OCR documentation and API reference for package instructions, licensing terms, and supported platforms before deployment.

Recognize Text from an Image with Tesseract

The standalone Tesseract package needs both the image and the trained data directory. This complete program shows the intended flow for an English image at images/receipt.png, with eng.traineddata in tessdata. The package version and platform-specific native dependencies were not supplied for verification, so treat this code as an unverified example and confirm its APIs and dependencies against the Tesseract project documentation for your target before compiling.

using System;
using Tesseract;

internal static class Program
{
    private static void Main()
    {
        const string imagePath = "images/receipt.png";
        const string tessdataPath = "tessdata";

        using (var engine = new TesseractEngine(tessdataPath, "eng", EngineMode.Default))
        using (var image = Pix.LoadFromFile(imagePath))
        using (var page = engine.Process(image))
        {
            Console.WriteLine(page.GetText());
        }
    }
}

For example, if the image visibly contains the line Invoice 1042, the program prints recognized text such as Invoice 1042 (followed by a newline). This is representative text, not an observed result; actual output depends on the image and recognition settings. Keep the tessdata directory with the deployed application, and change imagePath and the language code to match your input.

Recognize Text from an Image with IronOCR

IronOCR uses its own IronOcr namespace and API. The following full program uses IronTesseract, OcrInput.AddImage, and Read to recognize the same image and print the result. The package version was not supplied for verification, so this is an unverified example: check the exact signatures and dependencies in the IronOCR API reference and the IronOcr package instructions before compiling it.

using System;
using IronOcr;

internal static class Program
{
    private static void Main()
    {
        const string imagePath = "images/receipt.png";

        using (var ocr = new IronTesseract())
        using (var input = new OcrInput())
        {
            input.AddImage(imagePath);
            var result = ocr.Read(input);
            Console.WriteLine(result.Text);
        }
    }
}

If the image contains Invoice 1042, representative printed text is Invoice 1042 followed by a newline; this is illustrative, not an observed run. Install and reference IronOcr for these types: IronTesseract belongs to IronOCR and is not the standalone Tesseract engine. The text returned by result.Text is recognition output, so inspect or validate it before using it as trusted application data.

Improve OCR Accuracy and Handle Errors

Start with a sharp, well-lit image whose text has enough contrast and is not cut off. If results are poor, try a larger source image, straighten skewed pages, or preprocess a copy by converting it to grayscale, adjusting contrast, or reducing noise. Preprocessing can help some inputs and hurt others, so compare results using representative images from your application.

Configure the correct language: standalone Tesseract needs the matching trained data installed and an appropriate language code, while IronOCR’s language setup depends on the selected version. For multiple languages, configure the intended language data rather than assuming the default is sufficient.

Check that input files exist and are readable before processing, and handle library or file errors at the application boundary. Log useful context without exposing sensitive image contents. OCR can return partial or incorrect text without indicating that the result is suitable for downstream decisions; validate important fields and provide a review path when recognition is uncertain. Neither library example was executed in the supplied runtime evidence.