Recursive AI

OCR a 200 × 100 DPI fax in C#

OneOcr is a free, open-source OCR library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

A fax can have different horizontal and vertical DPI. Pass both when reading fax.tiff from the zip.

Terminal
dotnet add package RecursiveAI.OneOcr

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OneImage;
using OneOcr;
 
var image = Image.Load("fax.tiff");
var engine = OcrEngine.LoadBundled();
var page = engine.Ocr(image,
    new() { Dpi = 200, VerticalDpi = 100 });
Console.WriteLine(page.Text);
var code = OcrDiagnosticCodes.PixelsStretched;
Console.WriteLine(page.Diagnostics.Any(
    d => d.Code == code));

Unzip the example, then dotnet run.

Text
Fax notice
True

True means OneOcr stretched the page internally for recognition. RecursiveAI.OneOcr brings in OneImage, which supplies Image.Load.

The full program

The zip contains a synthetic, single-frame TIFF (480 × 120 pixels) labeled “Fax notice.” Here’s a PNG preview showing its stored pixels, without the stretch:

The words Fax notice, vertically compressed in the stored raster

The program reads the same pixels twice, first as square pixels and then at the actual fax density.

C#
using OneImage;
using OneOcr;
 
var engine = OcrEngine.LoadBundled();
var image = Image.Load("fax.tiff");
var square = engine.Ocr(image, new OcrOptions { Dpi = 200 });
var fax = engine.Ocr(image, new OcrOptions { Dpi = 200, VerticalDpi = 100 });
 
foreach (var (label, page) in new[] { ("200 x 200 DPI", square), ("200 x 100 DPI", fax) })
{
    bool stretched = page.Diagnostics.Any(d => d.Code == OcrDiagnosticCodes.PixelsStretched);
    Console.WriteLine($"{label}: stretched = {stretched}");
}
 
Console.WriteLine($"Original image: {fax.Width} x {fax.Height} pixels");
foreach (var word in fax.Words)
{
    var box = word.Box;
    Console.WriteLine(FormattableString.Invariant(
        $"{word.Text}: ({box.Left:0.#}, {box.Top:0.#}) to ({box.Right:0.#}, {box.Bottom:0.#})"));
}
Text
200 x 200 DPI: stretched = False
200 x 100 DPI: stretched = True
Original image: 480 x 120 pixels
Fax: (46, 42) to (98, 55)
notice: (117, 41.5) to (217, 55)

If stretching would exceed the OCR pixel limit, OneOcr reads the page as-is and reports PixelsNotStretched instead.

Variations

Read the DPI from the TIFF header. Use the file overload without a Dpi override when you trust the metadata. After loading engine as above:

C#
var page = engine.OcrPages("fax.tiff")[0];
Console.WriteLine(page.Diagnostics.Any(
    d => d.Code == OcrDiagnosticCodes.PixelsStretched));
Text
True

The short answer

Set OcrOptions.Dpi to the horizontal resolution and VerticalDpi to the vertical resolution, then call OcrEngine.Ocr to get the page and its word boxes. Omitting VerticalDpi on the already-decoded image in this example tells the engine to use 200 DPI on both axes. To keep an overlay aligned with the unmodified image, use the returned boxes directly rather than stretching their coordinates again.

What is the difference between horizontal and vertical DPI on a fax?

Horizontal DPI counts samples across a physical inch; vertical DPI counts samples down it. At 200 × 100 DPI, a 200-by-100-pixel region covers a square inch. A viewer that draws every pixel as a square shows that region twice as wide as it is tall. The effect is visible in the preview: the letters look shorter than on the page that was faxed.

For this sample OneOcr doubles the height internally, so it reads a 480 × 240 raster. It maps the boxes back before returning them; the Fax box ends at y = 55 in the 120-row input, not at y = 110 in the internal raster. Keep the original boxes if you draw on the original image. If you deliberately display the image at a different size, scale the boxes to match that display, just as you would with any OCR overlay.

Does changing the DPI of a scanned image help OCR?

Changing the declared DPI does not add detail that the scanner missed. Giving OneOcr the correct ratio lets it compensate for distorted letter shapes, not recover lost strokes. Setting both axes to 300 DPI on this fax would discard the information that its pixels are rectangular. If the image has already been resampled to square pixels, don’t apply the original fax ratio again.

The PixelsStretched diagnostic (OCR112) tells you whether a correction actually ran. OneOcr leaves nearly square images alone and warns rather than stretching when the declared ratio is beyond 4:1 or the stretched raster would exceed the pixel limit. For those decisions, see the stretch logic in OneOcr v1.1.0.

All oneocr recipes