Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / C# 3.5
Tip/Trick

Capture a Text Image on the Tracing Layer and Passing it to a Main Form (Using OCR MODI)

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
9 Sep 2015CPOL3 min read 12.5K   917   9  
New drag-and-drop intuitive engine on the OCR MODI base

Introduction

This technology allows you to highlight with a mouse some fragment of the image with a text and insert it at the same moment, there, where you will specify. There is a new, universal drag-and-drop method. Where not important, what you have as a source element (PictureBox, DataGridView, TextBox, ListBox, etc.), because on the top tracing layer, they appear as an image or picture. As for this example, we drag-and-drop from PictureBox to DataGridView, or from DataGridView to DataGridView, or from Button, or from Label ... controls. And the last - it is done intuitively and understandably even for children.

Image 1

How It Works

At first glance, everything looks simple enough. But there are five main points:

  1. Create the tracing Layer1 form in a separate thread, above the main Form1. For this click <Tracing Layer> button.
  2. Capture fragment of the image on the Layer1 form. To do this, draw mouse a closed curve around the needed text .
  3. Extract text from image using OCR MODI. For this click <Print> button Layer1. (Install Microsoft Office Document Imaging. For help, click <MODI Install> button.)
  4. Insert an extracting text into some dataGridView2 cell of the Form1. To do this, draw mouse a line from the selected text to some dataGridView2 cell. The last point of this line defines the cell where you will insert a text. Click <Insert> button Layer1.
  5. Click <Close> button Layer1 form and get the modified dataGridView2 of the Form1. It is the result!

Note: All event handlers work in a transparent Layer1 from above the main Form1. Only insert text raises the event in Form1. It was necessary to create these forms in separate threads.

Using the Code

Firstly, this program uses Microsoft Office Document Imaging. By default, Microsoft Office doesn't install it. You'll need to make sure that it's added by using the Microsoft Office installation program. Just run the installer, click on the Continue button with the "Add or Remove Features" selection made, and ensure that the imaging component is installed. Or, for help click <MODI Install> button Form1. After that, we can add to References>COM>Microsoft Office Document Imaging 12.0 Type Library.

Secondly, when loading Form1, you need to fill out list cell rectangles and point location of the dataGridView2. Why List<List<Rectangle>> construction? The List<Rectangle> is enumeration rows. The List<List<Rectangle>> is enumeration columns.

C#
private void Form1_Load(object sender, EventArgs e)
       {  ...
          ...
          ...

            //Create a 'List<dgv2Rectangles> _dgv2Rect'
           _dgv2Rect.dgv2pointLocation = dataGridView2.Location;

           //Template List<List<Rectangle>> by the columns
           List<List<Rectangle>> tempLR = new List<List<Rectangle>>();

           foreach (DataGridViewColumn column in dataGridView2.Columns)
           {
               int column_ind = dataGridView2.Columns[column.Name].Index;
               int row_count = dataGridView2.Rows.Count;
               Rectangle rct = new Rectangle();

               //Template List<Rectangle> by the rows dgv2
               List<Rectangle> tempR = new List<Rectangle>();
               for (int i = 0; i < row_count; i++)
               {
                   //Return the displayed portion of the cell only
                   rct = dataGridView2.GetCellDisplayRectangle(column_ind, i, true);
                   //All rectangles displayed by the rows in some column dgv2
                   tempR.Add(rct);
               }

               //All rectangles displayed in dgv2
               tempLR.Add(tempR);
           }

           //The 'List<List<Rectangle>>'
           _dgv2Rect.columnRectangles = tempLR;
       }

It is necessary for calculating a location last point of the leading line Layer1 above a dataGridView2 cell Form1. In other words, what is the row/column index cell where a text will be inserted.

Thirdly, create the tracing Layer1 form in a separate thread with event for passing date.

C#
...

 //Initialize component 'Layer1' and pass 'dgv2Rect' values
 Layer1 layer1 = new Layer1(_dgv2Rect);

 //Create handle the event for passing data between
 //two open forms 'Layer1' to 'Form1'
 layer1.TraceData += new Layer1.TraceDataEventHandler(layer1_TraceData);

 //Set position 'Layer1' form
 layer1.StartPosition = FormStartPosition.Manual;
 layer1.Left += xF;
 layer1.Top += yF + 24;
 //Set the preferences 'Layer1' form
 layer1.Width = this.Width;
 layer1.Height = this.Height;
 layer1.TopMost = true;
 layer1.Opacity = 0.4;
 layer1.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
 layer1.Show();

 ...

Fourthly, image capture takes place on the rectangle built on the points max / min of the List<Point> ptList1 (closed curve). Please, note : **** VERY IMPORTANT FRAGMENT:***** below.

C#
try
                {
                    //Max/Main value of points
                    int xMin = ptList1.Min(p => p.X);
                    if (xMin < 0) { xMin = 0; }
                    int xMax = ptList1.Max(p => p.X);
                    if (xMax > this.Width) { xMax = this.Width; }
                    int yMin = ptList1.Min(p => p.Y);
                    if (yMin < 0) { yMin = 0; }
                    int yMax = ptList1.Max(p => p.Y);
                    if (yMax > this.Height) { yMax = this.Height; }

                    //Width/Height drawing rectangle
                    int xWidth = xMax - xMin;
                    int yHeight = yMax - yMin;

                    Point locLayer1 = new System.Drawing.Point((int)Left, (int)Top);
                    Point locRect1 = new System.Drawing.Point(
                        (int)locLayer1.X + xMin, (int)locLayer1.Y + yMin);
                    locRec = new Point(locRect1.X - this.Location.X, locRect1.Y - this.Location.Y);
                    Rectangle Rect1 = new Rectangle(locRect1.X, locRect1.Y, xWidth, yHeight);

                    this.Hide();

                    string tempfile;
                    using (Bitmap bitmap = new Bitmap(Rect1.Width, Rect1.Height))
                    {
                        using (Graphics g = Graphics.FromImage(bitmap))
                        {
                            g.CopyFromScreen(locRect1, Point.Empty, Rect1.Size);
                        }

                        //*****VERY IMPORTANT FRAGMENT: *****
                        //DO NOT SAVE and REWRITE and DELETE by the const path file
                        //bitmap.Save(@"C:\Temp\test.jpg", ImageFormat.Jpeg);
                        //bitmap.Save(@"C:\Temp\test.tiff", ImageFormat.Tiff);
                        
                        //DO SAVE and DELETE by temp file
                        tempfile = Path.GetTempFileName();
                        bitmap.Save(tempfile, ImageFormat.Tiff);
                        //*****very important fragment *******
                        

                        //Releases all recourse 'bitmap'
                        bitmap.Dispose();
                    }

                    // Do Optical Character Recognition (OCR) MODI
                    DoOCR(tempfile);
                    Thread.Sleep(100);

                    this.Refresh();
                    this.Show();
                }
                catch (Exception exc) ...

Points of Interest

If OCR MODI has recognized a text (after clicking <Print>), you get a visible message like this:

Image 2

History

  • Released on 09-09-2015

References

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Israel Israel
8 years experience working as a Software Developer.
Use C#, Visual Basic, Java.

Comments and Discussions

 
-- There are no messages in this forum --