Cómo usar imágenes de System.Drawing para procesamiento de OCR en C#

How to Read from System.Drawing Objects

This article was translated from English: Does it need improvement?
Translated
View the article in English

System.Drawing.Bitmap is a class in the .NET Framework used for working with bitmap images. It provides methods and properties to create, manipulate, and display bitmap images.

System.Drawing.Image is a base class for all GDI+ image objects in the .NET Framework. It is the parent class for various image types, including System.Drawing.Bitmap.

IronSoftware.Drawing.AnyBitmap is a bitmap class in IronDrawing, an open-source library originally developed by Iron Software. It helps C# software engineers replace System.Drawing.Common in .NET projects on Windows, macOS, and Linux platforms.

Quickstart: Read Text from a System.Drawing.Bitmap

In just one simple statement, create an IronTesseract and feed it a System.Drawing.Bitmap wrapped by OcrImageInput to extract all text. This quickstart example shows how effortlessly IronOCR turns your image into readable text with minimal setup.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronOCR with NuGet Package Manager

    PM > Install-Package IronOcr

  2. Copy and run this code snippet.

    var result = new IronOcr.IronTesseract().Read(new IronOcr.OcrImageInput(new System.Drawing.Bitmap("image.png")));
  3. Deploy to test on your live environment

    Start using IronOCR in your project today with a free trial
    arrow pointer


Read System.Drawing.Bitmap Example

First, instantiate the IronTesseract class to perform OCR. Create a System.Drawing.Bitmap from one of the various methods. In the code example, I've decided to use a file path.

Next, use the using statement to create the OcrImageInput object, passing the image from the System.Drawing.Bitmap object to it. Finally, use the Read method to perform OCR.

:path=/static-assets/ocr/content-code-examples/how-to/input-system-drawing-read-bitmap.cs
using IronOcr;
using System.Drawing;

// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();

// Read image file to Bitmap
Bitmap bitmap = new Bitmap("Potter.tiff");

// Import System.Drawing.Bitmap
using var imageInput = new OcrImageInput(bitmap);
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);
Imports IronOcr
Imports System.Drawing

' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()

' Read image file to Bitmap
Private bitmap As New Bitmap("Potter.tiff")

' Import System.Drawing.Bitmap
Private imageInput = New OcrImageInput(bitmap)
' Perform OCR
Private ocrResult As OcrResult = ocrTesseract.Read(imageInput)
$vbLabelText   $csharpLabel

Read System.Drawing.Image Example

Reading from a System.Drawing.Image is as simple as creating the OcrImageInput object with the Image and then performing the standard OCR process using the Read method.

:path=/static-assets/ocr/content-code-examples/how-to/input-system-drawing-read-image.cs
using IronOcr;
using Image = System.Drawing.Image;

// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();

// Open image file as Image
Image image = Image.FromFile("Potter.tiff");

// Import System.Drawing.Image
using var imageInput = new OcrImageInput(image);
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);
Imports IronOcr
Imports Image = System.Drawing.Image

' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()

' Open image file as Image
Private image As Image = Image.FromFile("Potter.tiff")

' Import System.Drawing.Image
Private imageInput = New OcrImageInput(image)
' Perform OCR
Private ocrResult As OcrResult = ocrTesseract.Read(imageInput)
$vbLabelText   $csharpLabel

Read IronSoftware.Drawing.AnyBitmap Example

Similarly, after creating or obtaining an AnyBitmap object, you can construct the OcrImageInput class. The constructor will take care of all the necessary steps to import the data. The code example below demonstrates this.

:path=/static-assets/ocr/content-code-examples/how-to/input-system-drawing-read-anybitmap.cs
using IronOcr;
using IronSoftware.Drawing;

// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();

// Open image file as AnyBitmap
AnyBitmap anyBitmap = AnyBitmap.FromFile("Potter.tiff");

// Import IronSoftware.Drawing.AnyBitmap
using var imageInput = new OcrImageInput(anyBitmap);
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);
Imports IronOcr
Imports IronSoftware.Drawing

' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()

' Open image file as AnyBitmap
Private anyBitmap As AnyBitmap = AnyBitmap.FromFile("Potter.tiff")

' Import IronSoftware.Drawing.AnyBitmap
Private imageInput = New OcrImageInput(anyBitmap)
' Perform OCR
Private ocrResult As OcrResult = ocrTesseract.Read(imageInput)
$vbLabelText   $csharpLabel

Specify Scan Region

In the construction of the OcrImageInput class, you can specify the area to scan. This allows you to define the specific region of the image document for OCR. Depending on the image document, specifying the scanning region can significantly enhance performance. In the provided code example, I specify that only the chapter number and title should be extracted.

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

OCR Result

Read specific region

Preguntas Frecuentes

¿Cómo puedo extraer texto de una imagen bitmap en C#?

Puedes usar IronOCR creando primero una instancia de la clase IronTesseract. Luego, crea un System.Drawing.Bitmap y pásalo a un objeto OcrImageInput. Finalmente, usa el método Read para extraer texto.

¿Qué pasos están involucrados en la lectura de objetos System.Drawing para tareas de OCR?

Para leer de objetos System.Drawing con IronOCR, descarga la biblioteca, obtén objetos System.Drawing, construye la clase OcrImageInput, y define una región de recorte si es necesario. Usa AnyBitmap para la compatibilidad multiplataforma en Linux y macOS.

¿Cómo se realiza OCR en una System.Drawing.Image?

Para realizar OCR en una System.Drawing.Image, crea un objeto OcrImageInput con la imagen y ejecuta el método Read en IronOCR.

¿Cuál es el beneficio de usar AnyBitmap en proyectos multiplataforma?

IronSoftware.Drawing.AnyBitmap permite a los desarrolladores reemplazar System.Drawing.Common con una solución multiplataforma, habilitando funcionalidad OCR en Windows, macOS y Linux.

¿Cómo pueden las regiones de escaneo mejorar el rendimiento de OCR?

Al definir regiones de escaneo específicas en la clase OcrImageInput, puedes centrar los esfuerzos de OCR en áreas relevantes, lo que puede mejorar significativamente el rendimiento y la precisión.

¿Es IronOCR compatible con Linux y macOS?

Sí, IronOCR es compatible con Linux y macOS cuando se utiliza IronSoftware.Drawing.AnyBitmap, ofreciendo una solución multiplataforma para tareas de OCR.

¿Cómo especificas una región para escanear en una imagen para OCR?

En IronOCR, especifica una región de escaneo estableciendo coordenadas y dimensiones en la clase OcrImageInput, lo que mejora el rendimiento de OCR al centrarse en secciones de la imagen relevantes.

¿Qué clases son esenciales para manejar imágenes en proyectos OCR .NET?

En proyectos .NET OCR, clases como System.Drawing.Bitmap, System.Drawing.Image, y IronSoftware.Drawing.AnyBitmap son esenciales para el manejo de imágenes.

Curtis Chau
Escritor Técnico

Curtis Chau tiene una licenciatura en Ciencias de la Computación (Carleton University) y se especializa en el desarrollo front-end con experiencia en Node.js, TypeScript, JavaScript y React. Apasionado por crear interfaces de usuario intuitivas y estéticamente agradables, disfruta trabajando con frameworks modernos y creando manuales bien ...

Leer más
Revisado por
Jeff Fritz
Jeffrey T. Fritz
Gerente Principal de Programas - Equipo de la Comunidad .NET
Jeff también es Gerente Principal de Programas para los equipos de .NET y Visual Studio. Es el productor ejecutivo de la serie de conferencias virtuales .NET Conf y anfitrión de 'Fritz and Friends', una transmisión en vivo para desarrolladores que se emite dos veces a la semana donde habla sobre tecnología y escribe código junto con la audiencia. Jeff escribe talleres, presentaciones, y planifica contenido para los eventos de desarrolladores más importantes de Microsoft, incluyendo Microsoft Build, Microsoft Ignite, .NET Conf y la Cumbre de Microsoft MVP.
¿Listo para empezar?
Nuget Descargas 5,044,537 | Versión: 2025.11 recién lanzado