C# .NET - How to convert Image file(.jpg) into Text file(.txt) . My image file contains digits chars

Asked By hina samnani on 24-Aug-15 04:45 AM
I have attached image file now i want the same contents as in .jpg file into .txt file
Robbe Morris replied to hina samnani on 24-Aug-15 09:41 AM
I think you are going to need an OCR reader.  But if the image is simple enough, you might be able to simply convert the bytes.

using(MemoryStream stream = new MemoryStream())
{
  YourJPGBitMapHere
.Save (stream, ImageFormat.Png);
  var bytes = stream.GetBuffer();
  var text
= Convert.ToBase64String(bytes, Base64FormattingOptions.InsertLineBreaks);
}
hina samnani replied to Robbe Morris on 25-Aug-15 02:29 AM
But I want to convert it into .txt format so please suggest me how can i do this?
Robbe Morris replied to hina samnani on 25-Aug-15 08:13 AM
That code takes a byte array and converts it to text.  I'm just not certain it will work with your image.  Absent that, you'll need an OCR reader component.
Kev Mejana replied to hina samnani on 02-Sep-15 11:32 AM

As Robbe said you’ll need an OCR engine to do that. There are many samples and tutorials on the internet for using OCR in your applications. I found the following links that I think will help you get started:

http://www.codeproject.com/Articles/317163/OCR-with-LEADTOOLS-The-Better-Choice

https://gist.github.com/yatt/915443

http://www.codeproject.com/Articles/10130/OCR-with-Microsoft-Office


Hope this helps

hina samnani replied to Kev Mejana on 03-Sep-15 12:14 AM
Yes I tried "OCR-with-Microsoft-Office" but It gave me an Exception like 'OCR running error" My code is like below
Please suggest me how to solve this


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace SolReadTextFromImage
{
    class Program
    {
      static void Main(string[] args)
      {
        String ImagePath = @"D:\1.jpg";
        ReadTextFromImage(ImagePath);

        // Set Store Image Content text file Path  
        String StoreTextFilePath = AppDomain.CurrentDomain.BaseDirectory + "SampleText.txt";

        ReadTextFromImage(ImagePath, StoreTextFilePath);
      }
      private static void ReadTextFromImage(String ImagePath)  
      {  
        try  
        {  
          // Grab Text From Image  
          MODI.Document ModiObj = new MODI.Document();  
          ModiObj.Create(ImagePath);
          ModiObj.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, false,false);
          //Retrieve the text gathered from the image  
          MODI.Image ModiImageObj = (MODI.Image)ModiObj.Images[0];  
           
 
          System.Console.WriteLine(ModiImageObj.Layout.Text);  
 
          ModiObj.Close();  
        }  
        catch (Exception ex)  
        {  
          throw new Exception(ex.Message);  
        }  
      }   
      {  
        try  
        {  
          // Grab Text From Image  
          MODI.Document ModiObj = new MODI.Document();  
          ModiObj.Create(ImagePath);  
          ModiObj.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true);  
 
          //Retrieve the text gathered from the image  
          MODI.Image ModiImageObj = (MODI.Image)ModiObj.Images[0];  
           
 
          // Store Image Content in Text File  
          FileStream CreateFileObj = new FileStream(StoreTextFilePath, FileMode.Create);  
          //save the image text in the text file   
          StreamWriter WriteFileObj = new StreamWriter(CreateFileObj);  
          WriteFileObj.Write(ModiImageObj.Layout.Text);  
          WriteFileObj.Close();  
 
          ModiObj.Close();  
        }  
        catch (Exception ex)  
        {  
          throw new Exception(ex.Message);   
        }  
      }  
 
     

 
      
    }

}



Kev Mejana replied to hina samnani on 16-Sep-15 10:26 AM
Your MODI code looks alright, so the problem might be caused by something else like loading the image or some library reference. Make sure you have Office Document imaging installed properly. Also, check the exception message that you are getting and try to google it.