Image to text conversion in c# .net:
->Open visual studio 2010 crate new project from file option give project name as "image to text conversion"
-> Go to solution explorer right click on reference Add the Feature of "MODI" and Microsoft.Office.Tools.Word.v9.0.
-> Add a Form and design as Form1.cs shown below screen shot.
-> Click On Convert Button write code behind the Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Tools.Word;
using System.IO;
using System.Drawing.Imaging;
namespace imagetotextconvertion
{
public partial class Form1 : Form
{
List str = new List();
public Form1()
{
InitializeComponent();
}
// www.bitsbyta.com/2011/01/extracting-text-from-image-cnet.html
string extractedText = string.Empty;
string getFileName;
string fname = string.Empty;
string nme = string.Empty;
private void button1_Click(object sender, EventArgs e)
{
//Browsing an image
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
getFileName = openFileDialog1.FileName;
Image targetImage = Image.FromFile(getFileName);
targetImage = fitInPBox(targetImage);
pBox.Image = targetImage;
}
}
//This function fit the browsed image in picture box
private Image fitInPBox(Image img)
{
Bitmap image = new Bitmap(img, new Size(pBox.Size.Width, pBox.Size.Height));
return (Image)image;
}
private void button2_Click(object sender, EventArgs e)
{
MODI.Document doc = new MODI.Document();
doc.Create(getFileName);
MODI.Image img = (MODI.Image)doc.Images[0];
doc.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true);
MODI.Layout layout = img.Layout;
for (int i = 0; i < layout.Words.Count; i++)
{
MODI.Word word = (MODI.Word)layout.Words[i];
if (extractedText.Length > 0)
{
extractedText += " ";
}
extractedText += word.Text;
richTextBox1.Text = extractedText;
}
if (fname == "")
{
OpenFileDialog sfd = new OpenFileDialog();
sfd.Filter = "Text Files|*.txt";
DialogResult res = sfd.ShowDialog();
if (res == DialogResult.Cancel)
{
return;
}
fname = sfd.FileName;
// MessageBox.Show(fname);
}
StreamWriter sw = new StreamWriter(fname);
sw.WriteLine(richTextBox1.Text);
sw.Flush();
sw.Close();
richTextBox1.Text = "";
StreamReader sr = new StreamReader(fname);
string txt = sr.ReadToEnd();
string[] words = txt.Split(' ');
foreach (string word in words)
{
str.Add(word);
}
for (int i = 0; i < str.Count; i++)
{
string ki = str[i].Trim();
listBox1.Items.Add(ki);
}
OpenFileDialog skd = new OpenFileDialog();
skd.Filter = "Text Files|*.txt";
DialogResult rs = skd.ShowDialog();
if (rs == DialogResult.Cancel)
{
return;
}
fname = skd.FileName;
FileStream fs = new FileStream(fname, FileMode.OpenOrCreate, FileAccess.Write);
object[] a = new object[listBox1.Items.Count];
listBox1.Items.CopyTo(a, 0);
System.IO.StreamWriter file1 = new System.IO.StreamWriter(fs);
for (int k = 0; k < listBox1.Items.Count; k++)
{
file1.WriteLine(a[k]);
}
file1.Flush();
file1.Close();
fs.Close();
// dirty = false;
}
}
}
-> Run the Application window will be displayed
-> Click on Browse button and Select a Text Image.
-> Click on the Convert Button the text image converted as only text.
->Open visual studio 2010 crate new project from file option give project name as "image to text conversion"
-> Go to solution explorer right click on reference Add the Feature of "MODI" and Microsoft.Office.Tools.Word.v9.0.
-> Add a Form and design as Form1.cs shown below screen shot.
-> Click On Convert Button write code behind the Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Tools.Word;
using System.IO;
using System.Drawing.Imaging;
namespace imagetotextconvertion
{
public partial class Form1 : Form
{
List
public Form1()
{
InitializeComponent();
}
// www.bitsbyta.com/2011/01/extracting-text-from-image-cnet.html
string extractedText = string.Empty;
string getFileName;
string fname = string.Empty;
string nme = string.Empty;
private void button1_Click(object sender, EventArgs e)
{
//Browsing an image
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
getFileName = openFileDialog1.FileName;
Image targetImage = Image.FromFile(getFileName);
targetImage = fitInPBox(targetImage);
pBox.Image = targetImage;
}
}
//This function fit the browsed image in picture box
private Image fitInPBox(Image img)
{
Bitmap image = new Bitmap(img, new Size(pBox.Size.Width, pBox.Size.Height));
return (Image)image;
}
private void button2_Click(object sender, EventArgs e)
{
MODI.Document doc = new MODI.Document();
doc.Create(getFileName);
MODI.Image img = (MODI.Image)doc.Images[0];
doc.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true);
MODI.Layout layout = img.Layout;
for (int i = 0; i < layout.Words.Count; i++)
{
MODI.Word word = (MODI.Word)layout.Words[i];
if (extractedText.Length > 0)
{
extractedText += " ";
}
extractedText += word.Text;
richTextBox1.Text = extractedText;
}
if (fname == "")
{
OpenFileDialog sfd = new OpenFileDialog();
sfd.Filter = "Text Files|*.txt";
DialogResult res = sfd.ShowDialog();
if (res == DialogResult.Cancel)
{
return;
}
fname = sfd.FileName;
// MessageBox.Show(fname);
}
StreamWriter sw = new StreamWriter(fname);
sw.WriteLine(richTextBox1.Text);
sw.Flush();
sw.Close();
richTextBox1.Text = "";
StreamReader sr = new StreamReader(fname);
string txt = sr.ReadToEnd();
string[] words = txt.Split(' ');
foreach (string word in words)
{
str.Add(word);
}
for (int i = 0; i < str.Count; i++)
{
string ki = str[i].Trim();
listBox1.Items.Add(ki);
}
OpenFileDialog skd = new OpenFileDialog();
skd.Filter = "Text Files|*.txt";
DialogResult rs = skd.ShowDialog();
if (rs == DialogResult.Cancel)
{
return;
}
fname = skd.FileName;
FileStream fs = new FileStream(fname, FileMode.OpenOrCreate, FileAccess.Write);
object[] a = new object[listBox1.Items.Count];
listBox1.Items.CopyTo(a, 0);
System.IO.StreamWriter file1 = new System.IO.StreamWriter(fs);
for (int k = 0; k < listBox1.Items.Count; k++)
{
file1.WriteLine(a[k]);
}
file1.Flush();
file1.Close();
fs.Close();
// dirty = false;
}
}
}
-> Run the Application window will be displayed
-> Click on Browse button and Select a Text Image.
-> Click on the Convert Button the text image converted as only text.
Than i use your coding in my program than
ReplyDeleteusing Microsoft.Office.Tools.Word
List str = new List ();
Not taken properly give error..
How can add namespaces file
using Microsoft.Office.Tools.Word & use List .....