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.
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
Install with NuGet
Install-Package IronOcr
Download DLL
Manually install into your project
Install with NuGet
Install-Package IronOcr
Download DLL
Manually install into your project
Start using IronPDF in your project today with a free trial.
Check out IronOCR on Nuget for quick installation and deployment. With over 8 million downloads, it's transforming OCR with C#.
Install-Package IronOcr
Consider installing the IronOCR DLL directly. Download and manually install it for your project or GAC form: IronOcr.zip
Manually install into your project
Download DLLRead 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)
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)