How to Read Images
OCR, or Optical Character Recognition, is a technology that is used to recognize and extract text from images. This technology is particularly useful for digitizing printed documents, as it allows you to extract and work with the textual content from scanned pages, photographs, or other image files.
IronOCR supports various image formats, including jpg, png, gif, tiff, and bmp. Image filters are also available to enhance the reading capability.
Get started with IronOCR
Start using IronOCR in your project today with a free trial.
How to Read Images
- Download a C# library for reading images
- Support images in various formats, including jpg, png, gif, tiff, and bmp
- Instantiate the OcrImageInput class to input an image
- Use the
Read
method to perform OCR on the input image - Specify the crop region to define the reading area
Read Images Example
Begin by instantiating the IronTesseract class to enable OCR. Utilize the 'using' statement to create an OcrImageInput object, specifying the image file path. This ensures the proper disposal of resources when they are no longer needed. IronOCR supports input images in various formats, including jpg, png, gif, tiff, and bmp. Finally, use the Read
method to perform OCR.
:path=/static-assets/ocr/content-code-examples/how-to/input-images-read.cs
using IronOcr;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Add image
using var imageInput = new OcrImageInput("Potter.png");
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);
Imports IronOcr
' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()
' Add image
Private imageInput = New OcrImageInput("Potter.png")
' Perform OCR
Private ocrResult As OcrResult = ocrTesseract.Read(imageInput)
Visit the How to Read Multi-Frame/Page GIFs and TIFFs article to learn more about reading TIFF and GIF images.
Import Images as Bytes
Apart from the plain old filepath, the OcrImageInput class also accepts image information in the form of bytes, AnyBitmap, Stream, as well as Image. The AnyBitmap is a bitmap object of IronSoftware.Drawing.AnyBitmap.
:path=/static-assets/ocr/content-code-examples/how-to/input-images-import-byte.cs
using IronOcr;
using System.IO;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Read byte from file
byte[] data = File.ReadAllBytes("Potter.tiff");
// Import image byte
using var imageInput = new OcrImageInput(data);
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);
Imports IronOcr
Imports System.IO
' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()
' Read byte from file
Private data() As Byte = File.ReadAllBytes("Potter.tiff")
' Import image byte
Private imageInput = New OcrImageInput(data)
' Perform OCR
Private ocrResult As OcrResult = ocrTesseract.Read(imageInput)
Specify Scan Region
A CropRectangle is also accepted when instantiating the OcrImageInput class. This allows you to specify which region of the image document should be OCR'ed. Depending on the image document, specifying the region to scan can significantly improve performance. In the code example I provide, I specify that only the chapter number and title should be read.
:path=/static-assets/ocr/content-code-examples/how-to/input-images-read-specific-region.cs
using IronOcr;
using IronSoftware.Drawing;
using System;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Specify crop region
Rectangle scanRegion = new Rectangle(800, 200, 900, 400);
// Add image
using var imageInput = new OcrImageInput("Potter.tiff", 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()
' Specify crop region
Private scanRegion As New Rectangle(800, 200, 900, 400)
' Add image
Private imageInput = New OcrImageInput("Potter.tiff", ContentArea:= scanRegion)
' Perform OCR
Private ocrResult As OcrResult = ocrTesseract.Read(imageInput)
' Output the result to console
Console.WriteLine(ocrResult.Text)