Using The Office 2007 OCR Component in C#
2007-06-07
2007-06-07
| Table of Contents: |
| Rate This Article: | Add This Article To: |
Using The Office 2007 OCR Component in C# - ' The ShowBitmap Class'
( Page 5 of 5 )
( Page 5 of 5 )
The ShowBitmap Class
The ShowBitmap class displays the selected region and asks the user if they want to convert it to text. If they answer yes, a MODI object is instantiated and used to do the conversion. A window appears with the converted text.
// This Bitmap object stores the
// selected bitmap.
public Bitmap m_objBitmap;
// This just draws the bitmap in the window.
private void OnPaint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(m_objBitmap, 0, 0);
}
// We come here when the dialog has first appeared.
private void timer1_Tick(object sender, EventArgs e)
{
// Turn the timer off so we don't
// do this multiple times.
timer1.Enabled = false;
// Ask user if this is what they want to do.
if (MessageBox.Show("Convert this to text?",
"Convert?", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
// Create a temp file name.
string strFileName = "temp" + DateTime.Now.Millisecond +
".tif";
try
{
// Save the bitmap object to a tiff file.
m_objBitmap.Save(strFileName, ImageFormat.Tiff);
// Instantiate the MODI.Document object
MODI.Document md = new MODI.Document();
// The Create method grabs the picture from
// disk snd prepares for OCR.
md.Create(strFileName);
// Do the OCR.
md.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true);
// This string will contain the text.
string strText = String.Empty;
// Get the first (and only image)
MODI.Image image = (MODI.Image)md.Images[0];
// Get the layout.
MODI.Layout layout = image.Layout;
// Loop through the words.
for (int j = 0; j < layout.Words.Count; j++)
{
// Get this word.
MODI.Word word = (MODI.Word)layout.Words[j];
// Add a blank space to separate words.
if (strText.Length > 0)
{
strText += " ";
}
// Add the word.
strText += word.Text;
}
// Close the MODI.Document object.
md.Close(false);
// Create the dialog that displays
// the OCRed text.
ShowText st = new ShowText();
// The the dialog's text.
st.m_strOCRText = strText;
// Show the dialog.
st.ShowDialog();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
try
{
// Delete the temp file.
File.Delete(strFileName);
}
catch { }
}
// Close this dialog.
Close();
}
The complete project can be downloaded
Conclusion
While there is a considerable amount of code to this program, the OCR part of it is pretty short. It's really easy to do OCR with the MODI component. Give it a try, or download the demo program and experiment.
![]() |
|


