Click here to Skip to main content
15,910,661 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi respected programmers,

I have a small project where I have to convert an image(png format )into text.

Can anyone please help me?

Thanks in advance.

I have used this code:
C#
private string ConvertImage(Bitmap sBit)
{
    MemoryStream imageStream = new MemoryStream();
    //sBit.Save(imageStream, ImageFormat.Jpeg);
    sBit.Save(imageStream, System.Drawing.Imaging.ImageFormat.Png);
    return Convert.ToBase64String(imageStream.ToArray());
}

private void button1_Click(object sender, EventArgs e)
{
    Bitmap sBit = new Bitmap(@"C:\abc.png");
    string imageString = ConvertImage(sBit);
    // StreamWriter sw = new StreamWriter(@"C:\waleedelkot.text", false);
    StreamWriter sw = new StreamWriter(@"C:\wal.doc", false);
    sw.Write(imageString);
    sw.Close();
    MessageBox.Show("success");
}


It ran succesfully and resulted in doc file which contains this """"iVBORw0KGgoAAAANSUhEUgAAAMgAAAAPAQMAAACbexLRAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAAAZQTFRF////AgAAVkYHEAAAAG1JREFUKM9jYKAqYD7eZi9RIXH4+PwGNBm2nHPMFmcsj+XcYMCQEWOL4Kk6xnMfQya/jU2i4sbxAzfRTWN+c4xHgnfGseRGdBmGHJDMj2NpjJguOCYhcUbiWI4khmnH58+oq5D+fP48dUODaAAAFpUkO0wZp50AAAAASUVORK5CYII=""""" but my picture contains a numerical no 96171341725
Posted
Updated 10-Nov-11 4:52am
v5
Comments
Dalek Dave 10-Nov-11 3:32am    
Edited for Grammar and Spelling.
[no name] 10-Nov-11 4:12am    
i have used this code private string ConvertImage(Bitmap sBit) { MemoryStream imageStream = new MemoryStream(); //sBit.Save(imageStream, ImageFormat.Jpeg); sBit.Save(imageStream, System.Drawing.Imaging.ImageFormat.Png); return Convert.ToBase64String(imageStream.ToArray()); } private void button1_Click(object sender, EventArgs e) { Bitmap sBit = new Bitmap(@"C:\abc.png"); string imageString = ConvertImage(sBit); // StreamWriter sw = new StreamWriter(@"C:\waleedelkot.text", false); StreamWriter sw = new StreamWriter(@"C:\wal.doc", false); sw.Write(imageString); sw.Close(); MessageBox.Show("success"); } it ran succesfully and resulted in doc file which contains this """"iVBORw0KGgoAAAANSUhEUgAAAMgAAAAPAQMAAACbexLRAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAAAZQTFRF////AgAAVkYHEAAAAG1JREFUKM9jYKAqYD7eZi9RIXH4+PwGNBm2nHPMFmcsj+XcYMCQEWOL4Kk6xnMfQya/jU2i4sbxAzfRTWN+c4xHgnfGseRGdBmGHJDMj2NpjJguOCYhcUbiWI4khmnH58+oq5D+fP48dUODaAAAFpUkO0wZp50AAAAASUVORK5CYII=""""" but my picture contains a numerical no 96171341725
Rajesh Anuhya 10-Nov-11 5:59am    
your are looking for OCR kind of stuff???

 
Share this answer
 
Comments
Rajesh Anuhya 10-Nov-11 5:59am    
You got the point.... +5
RaisKazi 10-Nov-11 22:50pm    
Thank you Rajesh.
First of all, you don't seem to know that converting an image to a printable string (as base64, this is called serialization) is very different from OCR (mentioned above) which is the right way to go. I bet your image "shows" the number you mentioned. this number is displayed as pixel graphic. Serializing an image would take each pixel from the png (which is binary format) and convert it to, what you call a "string" (in your case base64, which is a string, free of white characters, means none printable characters). this is the nonsense you put into a .doc file (doesn’t work). OCR (optical character recognition) is the way to go, to extract or better guess (not convert) characters or strings from an pixel based image (like png). doing this is not a beginner task but you may try. just a few things to be mentioned: the office OCR needs an older office to work, in newer versions it is no more included, this info may safe you some time. there are a few articles around on codeproject that no longer work or have to be partially rewritten to work because apis or namespaces have changed over time or no longer exist anyway. so take the hints above and keep trying. It seems you are using XP (because you can write into c:\) maybe these older examples work on your desktop, but be aware they may not on W7 or Vista. Finally, I have working examples of OCR but it would take me hours to assemble an example for you, sorry. so keep trying. apart from that, a doc file has to have a certain structure, you cannot write a string into a doc file an expect word to open it correctly.
 
Share this answer
 
 
Share this answer
 
 
Share this answer
 
v2
Comments
[no name] 10-Nov-11 4:13am    
i have used this code
private string ConvertImage(Bitmap sBit) { MemoryStream imageStream = new MemoryStream(); //sBit.Save(imageStream, ImageFormat.Jpeg); sBit.Save(imageStream, System.Drawing.Imaging.ImageFormat.Png); return Convert.ToBase64String(imageStream.ToArray()); } private void button1_Click(object sender, EventArgs e) { Bitmap sBit = new Bitmap(@"C:\abc.png"); string imageString = ConvertImage(sBit); // StreamWriter sw = new StreamWriter(@"C:\waleedelkot.text", false); StreamWriter sw = new StreamWriter(@"C:\wal.doc", false); sw.Write(imageString); sw.Close(); MessageBox.Show("success"); } it ran succesfully and resulted in doc file which contains this """"iVBORw0KGgoAAAANSUhEUgAAAMgAAAAPAQMAAACbexLRAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAAAZQTFRF////AgAAVkYHEAAAAG1JREFUKM9jYKAqYD7eZi9RIXH4+PwGNBm2nHPMFmcsj+XcYMCQEWOL4Kk6xnMfQya/jU2i4sbxAzfRTWN+c4xHgnfGseRGdBmGHJDMj2NpjJguOCYhcUbiWI4khmnH58+oq5D+fP48dUODaAAAFpUkO0wZp50AAAAASUVORK5CYII="""""

but my picture contains a numerical no 96171341725
[no name] 10-Nov-11 4:13am    
private string ConvertImage(Bitmap sBit)
{
MemoryStream imageStream = new MemoryStream();
//sBit.Save(imageStream, ImageFormat.Jpeg);
sBit.Save(imageStream, System.Drawing.Imaging.ImageFormat.Png);
return Convert.ToBase64String(imageStream.ToArray());
}

private void button1_Click(object sender, EventArgs e)
{
Bitmap sBit = new Bitmap(@"C:\abc1.png");

string imageString = ConvertImage(sBit);

// StreamWriter sw = new StreamWriter(@"C:\waleedelkot.text", false);
StreamWriter sw = new StreamWriter(@"C:\wal.doc", false);
sw.Write(imageString);
sw.Close();

MessageBox.Show("success");
}

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