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

How can I read image streams for OCR processing in .NET C#?

In .NET C#, you can utilize IronOCR's import methods to read image streams by passing the stream data into the OcrImageInput constructor. This method efficiently handles the image data for OCR processing.

What is the benefit of using streams for OCR in IronOCR?

Using streams allows IronOCR to process large image files efficiently by handling data in smaller, manageable chunks. This approach optimizes memory usage and speeds up the OCR process.

How do I specify a region of an image to read for OCR in C#?

To specify a region of an image for OCR using IronOCR, you can use the CropRectangle class. Pass this as a parameter to the OcrImageInput constructor to focus on a specific area of the image.

Can IronOCR handle real-time image stream OCR?

Yes, IronOCR can handle real-time image stream OCR by continuously processing image data streams as they are received. This feature is beneficial for applications requiring immediate text recognition.

How can I improve OCR performance on large images?

To improve OCR performance on large images using IronOCR, specify a scan region with the CropRectangle class to limit processing to relevant areas, reducing the amount of data that needs to be analyzed.

What is the role of the OcrImageInput constructor in IronOCR?

The OcrImageInput constructor in IronOCR is used to import image streams and optionally define a specific region of interest for OCR processing. It streamlines the process of preparing images for text extraction.

How does the Read method function in IronOCR?

The Read method in IronOCR executes the optical character recognition process on the provided image stream. It extracts text from the image data and returns the recognized characters.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.
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