How to Read from Streams

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.

Get started with IronOCR

Start using IronOCR in your project today with a free trial.

First Step:
green arrow pointer



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)
$vbLabelText   $csharpLabel

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)
$vbLabelText   $csharpLabel

OCR Result

Read specific region

Frequently Asked Questions

What are streams in the context of programming?

Streams refer to a continuous flow of binary information that can be read from or written to. They allow efficient processing of data that may be too large to fit entirely in memory by handling data in smaller, manageable chunks.

How does the library handle image streams?

IronOCR can import image streams by passing the stream data into one of its import methods. The library handles all necessary steps to import and read the image for optical character recognition.

What is the first step to read from an image stream using the library?

The first step is to instantiate the IronTesseract class, which is the OCR engine used for reading from image streams.

How can you create a memory stream from an image using AnyBitmap?

You can convert an image file to an AnyBitmap object and then use its GetStream method to create a memory stream.

What is the benefit of specifying a scan region in the library?

Specifying a scan region using the CropRectangle class can improve performance on large images by focusing the OCR process on a specific area, thus obtaining specific readings from that region.

How do you define a specific region to read from an image stream in the library?

You define a specific region using the System.Drawing.Rectangle class to specify the coordinates and dimensions of the area to be read. This is then passed to the OcrImageInput constructor along with the image stream.

Can the library read text from large images efficiently?

Yes, IronOCR can read text from large images efficiently by processing the data in streams and allowing the specification of scan regions to focus on particular areas of interest.

Chaknith related to OCR Result
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.
Reviewed by
Jeff Fritz
Jeffrey T. Fritz
Principal Program Manager - .NET Community Team
Jeff is also a Principal Program Manager for the .NET and Visual Studio teams. He is the executive producer of the .NET Conf virtual conference series and hosts 'Fritz and Friends' a live stream for developers that airs twice weekly where he talks tech and writes code together with viewers. Jeff writes workshops, presentations, and plans content for the largest Microsoft developer events including Microsoft Build, Microsoft Ignite, .NET Conf, and the Microsoft MVP Summit