Image to Text Conversion in C#

watch_later 03 March, 2023

Converting an image to text can be a useful feature in many ASP.NET Core applications. Whether it's for extracting text from scanned documents, recognizing license plates from images, or processing receipts for expense tracking, the ability to convert images to text can save a lot of time and effort.

In my previous article, I explained Dependency Injection in ASP.NET Core 6 that you might like to read.

In this article, we'll explore how to convert an image to text in ASP.NET Core 6. We'll also provide a realistic example of how this feature can be used in an application.

Image to Text Conversion in ASP.NET Core 6

Before we dive into the code, let's first understand how image-to-text conversion works.

OCR (Optical Character Recognition) is the technology that allows us to convert an image to text. OCR engines use algorithms to analyze the image and identify characters and words. This technology has come a long way in recent years, and some OCR engines can recognize handwritten text and even recognize text from multiple languages.

There are several OCR engines available, some of which are open-source, while others are commercial products. In this article, we'll be using the Tesseract OCR engine, which is an open-source OCR engine that is widely used and supported by a large community.

Step 1: Setting up the project

To get started, let's create a new ASP.NET Core 6 project in Visual Studio. We'll use ASP.NET Core MVC for this example, but the same principles can be applied to other ASP.NET Core frameworks.

Step 2: Adding the Tesseract OCR engine

We need to download and add the Tesseract OCR engine to our project. You can download the latest version of the Tesseract OCR engine from GitHub.

Once you've downloaded the Tesseract OCR engine, extract the files and copy them to a folder in your project. For example, you could create a folder named "Tesseract" in your project and copy the files to that folder.

Step 3: Adding the Tesseract OCR wrapper

The Tesseract OCR engine is written in C++, so we need to use a wrapper to call the OCR engine from our ASP.NET Core application. One popular wrapper for Tesseract OCR is the Tesseract-OCR-for-.NET library, which provides a .NET wrapper around the Tesseract OCR engine.

You can download the Tesseract-OCR-for-.NET library from NuGet. To add the library to your project, right-click on the project in Visual Studio and select "Manage NuGet Packages." Search for "tesseract-ocr-for-net" and install the latest version.

Step 4: Creating the image upload form

Next, let's create an image upload form that allows users to upload an image for conversion. We'll use the ASP.NET Core Tag Helpers to create the form.

Here's the code for the image upload form:

<form method="post" enctype="multipart/form-data">
    <div class="form-group">
        <label for="imageFile">Select an image file:</label>
        <input type="file" class="form-control-file" id="imageFile" name="imageFile">
    </div>
    <button type="submit" class="btn btn-primary">Convert to Text</button>
</form>
 
<div class="mt-3">
    @if (ViewBag.ConvertedText != null)
    {
        <h4>Converted Text:</h4>
        <p>@ViewBag.ConvertedText</p>
    }
</div>

This code creates a form that allows users to select an image file using a file input field. We also add a button that triggers the conversion process when clicked. Finally, we add a section to display the converted text.

Step 5: Writing the conversion code

Now that we have the image upload form, let's write the code that converts the image to text. We'll use the Tesseract-OCR library to call the Tesseract OCR engine and extract text from the uploaded image.

Here's the code for the conversion process:

[HttpPost]
public async Task<IActionResult> ConvertImageToText(IFormFile imageFile)
{
    if (imageFile == null || imageFile.Length == 0)
    {
        ViewBag.ConvertedText = "Please select an image file.";
        return View("Index");
    }
 
    var filePath = Path.Combine(_hostEnvironment.ContentRootPath, "uploads", imageFile.FileName);
 
    using (var stream = new FileStream(filePath, FileMode.Create))
    {
        await imageFile.CopyToAsync(stream);
    }
 
    using (var engine = new TesseractEngine(Path.Combine(_hostEnvironment.ContentRootPath, "tessdata"), "eng"))
    {
        using (var img = Pix.LoadFromFile(filePath))
        {
            using (var page = engine.Process(img))
            {
                ViewBag.ConvertedText = page.GetText();
            }
        }
    }
 
    return View("Index");
}

Let's break down this code.

First, we check if an image file has been uploaded. If no file has been uploaded, we display an error message and return to the index view.

If a file has been uploaded, we save it to a folder named "uploads" in our project directory.

Next, we create a new TesseractEngine object and specify the path to the tessdata folder (where the OCR engine data is stored) and the language we want to use (in this case, English).

We load the image file using the Pix.LoadFromFile method and then process the image using the TesseractEngine.Process method. This method returns a Page object that contains the recognized text.

Finally, we display the converted text in the index view using the ViewBag.

Step 6: Running the application

We're now ready to run our application and test the image-to-text conversion feature.

When we run the application, we'll see the image upload form. We can select an image file and click the "Convert to Text" button to trigger the conversion process.

Once the conversion process is complete, the converted text will be displayed below the image upload form.

Realistic Example

Let's consider a realistic example of how the image-to-text conversion feature can be used in an ASP.NET Core application.

Suppose we're building an expense tracking application that allows users to upload receipts and automatically extract the transaction details from the receipt. To achieve this, we can use the image-to-text conversion feature to extract the text from the uploaded receipt image.

We can then use the extracted text to populate the transaction details, such as the date, merchant name, and transaction amount. This can save a lot of time and effort for the user, as they no longer need to manually enter the transaction details.

People also searching for:

  • Image to text conversion in ASP.NET Core 6
  • Converting images to text using Tesseract OCR in ASP.NET Core 6
  • ASP.NET Core 6 image to text converter
  • OCR engine integration in ASP.NET Core 6
  • How to convert an image to text in ASP.NET Core 6
  • Tesseract-OCR-for-.NET in ASP.NET Core 6
  • Image recognition in ASP.NET Core 6
  • ASP.NET Core 6 image processing and text extraction
  • Server-side image to text conversion in ASP.NET Core 6
  • Building an image to text converter with ASP.NET Core 6 and Tesseract OCR.

Conclusion

In this article, we explored how to convert an image to text in an ASP.NET Core 6 application using the Tesseract OCR engine and the Tesseract-OCR-for-.NET library. We also provided a realistic example of how this feature can be used in an application.

By using image-to-text conversion, we can automate the process of extracting text from images and save a lot of time and effort.

Tags:

  • image to text
  • online ocr
  • image converter
  • image convert to pdf
  • image to text converter
  • extract text from image
  • photo to text converter
  • picture to text converter
  • get text from image
  • read text from image
  • image ocr
  • picture to text
  • photo to text
  • pdf ocr
  • pdf to word ocr
  • photo converter
  • free online ocr
  • free ocr
  • convert image to word
  • ocr converter
  • text to image
  • translate image text
  • free ocr software
  • convert to text
  • convert pdf to text
  • text to pdf converter
  • picture text
  • translate picture to text
  • picture convert to pdf
  • pdf convert to image
  • photo into text
  • convert picture
  • software image
  • extract text
  • convert to words
  • images in text
  • pdf converter photo
  • image to text converter online
  • image to text google
  • image to text online
  • ocr image to text
  • pdf image to text
  • online image to text converter
  • ocr text
  • ocr to word
  • pdf ocr online
  • online ocr converter
  • online ocr pdf to word
  • google image to text
  • convert pdf to word ocr
  • text recognition from image
  • pdf to ocr converter
  • convert text to image
  • ocr translate
  • pdf to word ocr free
  • extract text from image online
  • picture to text online
  • extract words from image
  • pdf to word converter ocr
  • convert pdf image to text
  • online image converter
  • word to image converter
  • pdf to text ocr
  • free image converter
  • pdf to image converter online
  • photo to word converter
  • translate image to text online
  • image text to text
  • image file converter
  • picture to word converter
  • image to word converter online
  • photo to text converter online
  • translate photo to text
  • convert pdf to picture
  • free ocr pdf
  • image pdf to word
  • ocr recognition
  • extract text from pdf image
  • image to text converter online free
  • convert image text to word
  • text to picture
  • free image to text converter
  • online convert image to pdf
  • ocr text recognition
  • free ocr converter
  • image in text
  • pdf ocr software
  • pdf to text converter online
  • text on image online
  • image ocr online
  • convert text in image to text
  • word to text converter
  • convert picture to word text
  • text to word converter
  • extract text from image google
  • convert text file to pdf
  • pdf to text converter free
  • extract text from picture
  • text to pdf converter online
  • convert pdf to text free
  • how to get text from an image in c#
  • how to make ocr in c#
  • convert image to text ocr free
  • extract text from image in c#
  • read text from image c# without ocr
  • c# ocr image to text free
  • ironocr example c#
  • ocr c# code project
  • how to convert an image to text in c#
  • how can i convert image to text
  • photo to text converter online
  • convert picture to text in word
  • image to text free
  • image to text converter google
  • jpg to text converter
  • extract text from image
  • image to text translator
  • pdf to text converter
  • how can i convert image to text
  • how do i turn a pdf image into text
  • which ai can convert image to text
  • image text copy
  • edit image text
  • image write text
  • extract text from image c# tesseract
  • can you convert image to text

Codingvila provides articles and blogs on web and software development for beginners as well as free Academic projects for final year students in Asp.Net, MVC, C#, Vb.Net, SQL Server, Angular Js, Android, PHP, Java, Python, Desktop Software Application and etc.

Thank you for your valuable time, to read this article, If you like this article, please share this article and post your valuable comments.

Once, you post your comment, we will review your posted comment and publish it. It may take a time around 24 business working hours.

Sometimes I not able to give detailed level explanation for your questions or comments, if you want detailed explanation, your can mansion your contact email id along with your question or you can do select given checkbox "Notify me" the time of write comment. So we can drop mail to you.

If you have any questions regarding this article/blog you can contact us on info.codingvila@gmail.com

sentiment_satisfied Emoticon