How to Read from Streams

by Chaknith Bin

Stream data refers to a continuous flow of binary information that can be read from or written to. In the context of programming and data handling, streams are used to efficiently process data that may be too large to fit entirely in memory. Streams allow data to be read or written in smaller, manageable chunks.

IronOcr's import methods also accept data streams of images to be imported and read. This can be done by simply passing the stream data into one of the import methods. The method will handle all the necessary steps to import the image.


C# NuGet Library for OCR

Install with NuGet

Install-Package IronOcr
or
C# OCR DLL

Download DLL

Download DLL

Manually install into your project

Read Streams Example

First, instantiate the IronTesseract class to perform OCR. Use the FromFile method of AnyBitmap to import the image file. This AnyBitmap object will be able to convert the image data into a stream. Next, use the 'using' statement to create the OcrImageInput object by passing the image stream with the GetStream method of the AnyBitmap object. Finally, use the Read method to perform OCR.

:path=/static-assets/ocr/content-code-examples/how-to/input-streams-read-streams.cs
using IronOcr;
using IronSoftware.Drawing;

// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();

// Read image file to AnyBitmap
AnyBitmap anyBitmap = AnyBitmap.FromFile("Potter.tiff");

// Import image stream
using var imageInput = new OcrImageInput(anyBitmap.GetStream());
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);
Imports IronOcr
Imports IronSoftware.Drawing

' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()

' Read image file to AnyBitmap
Private anyBitmap As AnyBitmap = AnyBitmap.FromFile("Potter.tiff")

' Import image stream
Private imageInput = New OcrImageInput(anyBitmap.GetStream())
' Perform OCR
Private ocrResult As OcrResult = ocrTesseract.Read(imageInput)
VB   C#

Specify Scan Region

To improve performance on large images and obtain specific readings from certain regions, you can utilize the CropRectangle class. The OcrImageInput constructor accepts a CropRectangle object as a second parameter. This allows you to specify which region of the image document should be read. In the code example below, I specify that only the chapter number and title region should be read.

:path=/static-assets/ocr/content-code-examples/how-to/input-streams-read-specific-region.cs
using IronOcr;
using IronSoftware.Drawing;
using System;

// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();

// Read image file to AnyBitmap
AnyBitmap anyBitmap = AnyBitmap.FromFile("Potter.tiff");

// Specify crop region
Rectangle scanRegion = new Rectangle(800, 200, 900, 400);

// Add image
using var imageInput = new OcrImageInput(anyBitmap.GetStream(), ContentArea: scanRegion);
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);

// Output the result to console
Console.WriteLine(ocrResult.Text);
Imports IronOcr
Imports IronSoftware.Drawing
Imports System

' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()

' Read image file to AnyBitmap
Private anyBitmap As AnyBitmap = AnyBitmap.FromFile("Potter.tiff")

' Specify crop region
Private scanRegion As New Rectangle(800, 200, 900, 400)

' Add image
Private imageInput = New OcrImageInput(anyBitmap.GetStream(), ContentArea:= scanRegion)
' Perform OCR
Private ocrResult As OcrResult = ocrTesseract.Read(imageInput)

' Output the result to console
Console.WriteLine(ocrResult.Text)
VB   C#

OCR Result

Read specific region

Chaknith Bin

Software Engineer

Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking.