C# OCR (Optical Character Recognition)

OCR as the title say stands for: Optical Character Recognition, the ability to extract characters as they appear in an image.


We will be using the MODI Type library, it's a COM Interop.


The MODI library is available within The Microsoft Office suites (2003 to 2007), Unfortunately it is not available in the 2010 version.




Include the MODI Type library (COM Interop) and convert image(s) to text like this:
 
using MODI;
using System;
 
class Program
{
    static void Main(string[] args)
    {
        DocumentClass myDoc = new DocumentClass();
        myDoc.Create(@"theDocumentName.tiff"); //we work with the .tiff extension
        myDoc.OCR(MiLANGUAGES.miLANG_ENGLISH, true, true);
 
        foreach (Image anImage in myDoc.Images)
        {
            Console.WriteLine(anImage.Layout.Text); //here we cout to the console.
        }
    }
}






Leave me a comment if you need help with it.

HOW TO COPY A FILE USING C#

Hi, in this post I'll explain you an easy way of copy paste a file using the C# language.
The next example will be shown using Microsoft Visual Studio 2008, but this example can be done with whatever ide you prefer.


STEP 1: Create a Console Application. (for quick demonstration purposes)

STEP 2: THE CODE.



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

namespace HOWTO_COPY
{
    class Program
    {
        static void Main(string[] args)
        {
            //the try below will manage IOExceptions.
            try
            {
                //creates and fill the bytes array with the data.
                byte[] readBuffer = System.IO.File.ReadAllBytes(@"C:\directory\ORIGINAL-FILE.txt");

                //finally we do the writing of the bytes array back to a new file.
                //(this will overwrite any existing file matching the filename).
                System.IO.File.WriteAllBytes(@"C:\directory\COPIED-FILE.txt", readBuffer);
                Console.WriteLine("FILE SUCCESSFULLY CREATED");
            }
            catch (System.IO.IOException e)
            {
                Console.WriteLine(e.Message);
            }
            //the lines below will freeze the console so you see the output.
            Console.WriteLine("\n\nEND - | C# Quick Examples |");
            Console.ReadLine();
        }
    }
}

SCREENSHOTS

Create the Console Application Project.




Only one file in that directory "C:\directory\"

Running the Application.
File successfully copied, same size, (the data is also the same).



See you in the next example.

Display a Table using MySQL databases.


In this post, I'll show you an easy way of gathering data from a table using the C# programming language and
a MySQL database.
 
There are several ways of connecting to a database, I'll cover other ways in future posts.





What you will need for this example to work:


1-Microsoft Visual Studio (any version 2005,2008,2010,Express).

2-MySQL Connector (it's not distributed by Microsoft). It's opensource and free.
This will let you access a MySQL database, locally or remotely from your C# application.
download it from here mysql.data.msi 10MB then INSTALL IT!.



3-Create a Windows Console Application.
(the example below uses a console application for quicker demonstration purposes).












4-Add the refference: Go to Project > Add Refference...








5- After that add the line below to the top of your code.


using MySql.Data.MySqlClient;




note:

The database I'm using is called "test".
The Table I'm using is called "people" and looks like this.





6- The code is self-explanatory.

Make sure you put the next lines inside your MainMethod.


try
       {
           //Sets a wellformed connection string (domain;database name;user;EmtyPasswordInmyCase)
           string connectionString = @"Server=127.0.0.1;
                                            Database=test;
                                            Uid=root;
                                            Pwd=;
                                            ";
   //Creates the MySqlConnection Object
   MySqlConnection connection = new                  
   MySqlConnection(connectionString);

   //Creates the MySqlCommand query
   MySqlCommand command = connection.CreateCommand();
   command.CommandText = "SELECT * FROM people";

   //Creates the DataAdapter "dataAdapter"
   MySqlDataAdapter dataAdapter = new MySqlDataAdapter(command);
   DataSet dataSet = new DataSet();

   //Fill the MySqlDataAdapter object "dataAdapter"
   // with data from People.
   dataAdapter.Fill(dataSet, "People");

   //Creates the DataTable object  from the previous loaded DataAdapter object
   //to manipulate the data from the People table.
   DataTable builtTable = dataSet.Tables["People"];
               
   //PRINTS THE COLUMNS TITLES
   foreach (DataColumn dc in builtTable.Columns)
         Console.Write(dc+" | ");

   //A NEW LINE
   Console.WriteLine("");

   //Loop the People Table
   foreach (DataRow row in builtTable.Rows)
    {
         foreach (var field in row.ItemArray)
           {
               Console.Write(field + "  |");
           }
          Console.WriteLine("");
    }
  }//close try
  //catches any exception related to MySql and displays it
  catch (MySqlException me)
      {
         Console.WriteLine(me.Message);
      }
           
      //end
      Console.WriteLine("FINISHED.");
      Console.ReadKey();





FINISHED



note: I used a console application for this example, But it is always a good practice to keep the logic away from the interface. In this case the console is my interface.
It is recommended to pass the table to the interface, a Windows Form GUI for example or the console.



If you found this example useful share it with others.




TIP: If you are looking for a free web host with MySQL capabilities.
1500 MB of space.
100 GB of bandwith traffic.
MySQL + PHP
and a lot more great stuff...

You are supporting this blog by using their free service if you click and register below.
thanks :) click here.

If you want to learn more about it "check my 000webhost introductory post".











2 ads