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.
How to Read from Streams
- Download a C# library for reading from streams
- Obtain and prepare the image stream data
- Pass the image stream to the OcrImageInput constructor to import the image
- Use the
Read
method to perform OCR - Define the reading area by specifying the crop region
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;
// This code demonstrates how to perform Optical Character Recognition (OCR)
// on an image file using the IronTesseract library.
// Create an instance of IronTesseract to perform OCR.
IronTesseract ocrTesseract = new IronTesseract();
try
{
// Load the image file into an AnyBitmap instance.
// AnyBitmap is a compatible bitmap format used by IronOcr to process images.
AnyBitmap anyBitmap = AnyBitmap.FromFile("Potter.tiff");
// Process the image stream with IronOcr.
// Use a 'using' statement to ensure that the OcrImageInput is properly disposed of afterward.
using (var imageInput = new OcrImageInput(anyBitmap.GetStream()))
{
// Perform OCR on the provided image input.
// The Read method does the actual OCR processing and returns an OcrResult object.
OcrResult ocrResult = ocrTesseract.Read(imageInput);
// Output the recognized text
Console.WriteLine(ocrResult.Text);
}
}
catch (Exception ex)
{
// Handle exceptions that may occur, such as file not found or OCR processing errors.
Console.WriteLine("An error occurred: " + ex.Message);
}
Imports IronOcr
Imports IronSoftware.Drawing
' This code demonstrates how to perform Optical Character Recognition (OCR)
' on an image file using the IronTesseract library.
' Create an instance of IronTesseract to perform OCR.
Private ocrTesseract As New IronTesseract()
Try
' Load the image file into an AnyBitmap instance.
' AnyBitmap is a compatible bitmap format used by IronOcr to process images.
Dim anyBitmap As AnyBitmap = AnyBitmap.FromFile("Potter.tiff")
' Process the image stream with IronOcr.
' Use a 'using' statement to ensure that the OcrImageInput is properly disposed of afterward.
Using imageInput = New OcrImageInput(anyBitmap.GetStream())
' Perform OCR on the provided image input.
' The Read method does the actual OCR processing and returns an OcrResult object.
Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)
' Output the recognized text
Console.WriteLine(ocrResult.Text)
End Using
Catch ex As Exception
' Handle exceptions that may occur, such as file not found or OCR processing errors.
Console.WriteLine("An error occurred: " & ex.Message)
End Try
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;
using System.Drawing;
// This example demonstrates how to use IronOcr library to extract text from a specified region of an image.
// Instantiate IronTesseract, which is a class from the IronOCR library used to perform OCR operations.
IronTesseract ocrTesseract = new IronTesseract();
try
{
// Read image file to AnyBitmap. Ensure the file path to 'Potter.tiff' is correct.
AnyBitmap anyBitmap = AnyBitmap.FromFile("Potter.tiff");
// Define the crop region. The values represent: x, y, width, height.
// This restricts the OCR operation to the specified area in the image.
Rectangle scanRegion = new Rectangle(800, 200, 900, 400);
// Add image and crop it to the specified ContentArea.
// 'using' ensures the OcrImageInput object is disposed of correctly after use.
using OcrInput imageInput = new OcrInput(anyBitmap.GetStream())
{
ContentArea = scanRegion
};
// Perform OCR on the image input within the specified region.
OcrResult ocrResult = ocrTesseract.Read(imageInput);
// Output the recognized text from the image to the console.
Console.WriteLine(ocrResult.Text);
}
catch (Exception ex)
{
// Output any errors that might have occurred to the console.
Console.WriteLine($"An error occurred: {ex.Message}");
}
Imports IronOcr
Imports IronSoftware.Drawing
Imports System
Imports System.Drawing
' This example demonstrates how to use IronOcr library to extract text from a specified region of an image.
' Instantiate IronTesseract, which is a class from the IronOCR library used to perform OCR operations.
Private ocrTesseract As New IronTesseract()
Try
' Read image file to AnyBitmap. Ensure the file path to 'Potter.tiff' is correct.
Dim anyBitmap As AnyBitmap = AnyBitmap.FromFile("Potter.tiff")
' Define the crop region. The values represent: x, y, width, height.
' This restricts the OCR operation to the specified area in the image.
Dim scanRegion As New Rectangle(800, 200, 900, 400)
' Add image and crop it to the specified ContentArea.
' 'using' ensures the OcrImageInput object is disposed of correctly after use.
Using imageInput As New OcrInput(anyBitmap.GetStream()) With {.ContentArea = scanRegion}
' Perform OCR on the image input within the specified region.
Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)
' Output the recognized text from the image to the console.
Console.WriteLine(ocrResult.Text)
End Using
Catch ex As Exception
' Output any errors that might have occurred to the console.
Console.WriteLine($"An error occurred: {ex.Message}")
End Try
OCR Result

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 IronOCR 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 IronOCR?
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 IronOCR?
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 IronOCR?
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 IronOCR 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.