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.
How It Works
At first glance, everything looks simple enough. But there are five main points:
- Create the tracing
Layer1
form in a separate thread, above the main Form1
. For this click <Tracing Layer>
button.
- Capture fragment of the image on the
Layer1
form. To do this, draw mouse a closed curve around the needed text .
- Extract text from image using OCR MODI. For this click
<Print>
button Layer1
. (Install Microsoft Office Document Imaging. For help, click <MODI Install>
button.)
- 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
.
- 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.
private void Form1_Load(object sender, EventArgs e)
{ ...
...
...
_dgv2Rect.dgv2pointLocation = dataGridView2.Location;
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();
List<Rectangle> tempR = new List<Rectangle>();
for (int i = 0; i < row_count; i++)
{
rct = dataGridView2.GetCellDisplayRectangle(column_ind, i, true);
tempR.Add(rct);
}
tempLR.Add(tempR);
}
_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.
...
Layer1 layer1 = new Layer1(_dgv2Rect);
layer1.TraceData += new Layer1.TraceDataEventHandler(layer1_TraceData);
layer1.StartPosition = FormStartPosition.Manual;
layer1.Left += xF;
layer1.Top += yF + 24;
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.
try
{
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; }
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);
}
tempfile = Path.GetTempFileName();
bitmap.Save(tempfile, ImageFormat.Tiff);
bitmap.Dispose();
}
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:
History
References