Click here to Skip to main content
15,886,797 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
heloo to everyone

i have a project where i have to convert png images into text
i have a code . this works fine with jpg image but i dont know why its not working properly with png image.

i will be thankful to you
C#
public partial class Form1 : Form
    {
        string extractedText = string.Empty;
        string getFileName;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {

                getFileName = openFileDialog1.FileName;

                Image targetImage = Image.FromFile(getFileName);

                targetImage = fitInPBox(targetImage);

                pBox.Image = targetImage;

            }

        }
        private Image fitInPBox(Image img)
        {
            
            
            Bitmap image = new Bitmap(img, new Size(pBox.Size.Width, pBox.Size.Height));

            return (Image)image;
        }

        private void btnExtract_Click(object sender, EventArgs e)
        {
            MODI.Document doc = new MODI.Document();
            doc.Create(getFileName);
            
            
               
                    try
                    {
                       
                    doc.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true);
                   
                    }
                    catch { }

            
            MODI.Image img = (MODI.Image)doc.Images[0];
            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;
            }

        }


    }
}


here is my code
Posted
Updated 10-Nov-11 4:31am
v2
Comments
UJimbo 10-Nov-11 10:36am    
Why repost the question? Also, what's wrong with you posting on a respectable site with that nickname, be it a phallus or a rooster I don't care. This is unacceptable
[no name] 10-Nov-11 10:38am    
i didnt understand what do u mean. what the problemin my nick name
Manfred Rudolf Bihy 10-Nov-11 10:48am    
Just go to OPs profile page and report as Spammer\Abusive. A whole bunch of members already got the boot for using abusive names. Once a member has been reported 5 times the account is automatically deactivated.
[no name] 10-Nov-11 10:50am    
can u plz tell me what actually my nickname COCKSURE77 means.
as far as i know it doesnot have any wrong/abusive meaning as u r thhinking
if iam wrong plz do tell me teh meaning of COCKSURE
Sergey Alexandrovich Kryukov 10-Nov-11 11:41am    
Guys, please relax. The guy is from India. Stop measuring everything with your Western standards. A name is a name. Many names in some languages sound like taboo words in others.
--SA

1 solution

Anyway, i'm not going to take it personal with your nick name and the like but please do take advise.
Below is the code i would use to convert an image to text, well basically to it's base64 representation, that's if i got your question right:

C#
try
{
    
    System.Drawing.Image img = System.Drawing.Image.FromFile( "img path here" );
    ImageFormat format = ImageFormat.Png;
    MemoryStream memory = new MemoryStream();
    img.Save( memory, format );
    string base64 = Convert.ToBase64String( memory.ToArray() );
    memory.Close();
    File.WriteAllText( "img.txt", base64 );
    Console.WriteLine( "data written to the chosen text file" );
}
catch ( Exception ex )
{
    Console.WriteLine( ex );
}


This works for me.
Happy coding,
Morgs
 
Share this answer
 
Comments
dreamypritom 2-Aug-12 14:47pm    
where your main method???? Which algorithm you used????

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900