Created
April 10, 2013 13:58
-
-
Save tufanbarisyildirim/5354898 to your computer and use it in GitHub Desktop.
OCR Extension metod for System.Drawing.Image using MODI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
using System.Drawing; | |
using System.IO; | |
using System.Drawing.Imaging; | |
namespace Extensions | |
{ | |
public static class ImageExtensions | |
{ | |
public static string ExtractText(this Image image) | |
{ | |
var tmpFile = Path.GetTempFileName(); | |
string text = null; | |
try | |
{ | |
var bmp = new Bitmap(Math.Max(image.Width, 1024), Math.Max(image.Height, 768)); | |
var gfxResize = Graphics.FromImage(bmp); | |
gfxResize.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height)); | |
bmp.Save(tmpFile + ".bmp", ImageFormat.Bmp); | |
var doc = new MODI.Document(); | |
doc.Create(tmpFile + ".bmp"); | |
doc.OCR(MODI.MiLANGUAGES.miLANG_TURKISH, false, true); | |
var img = (MODI.Image)doc.Images[0]; | |
var layout = img.Layout; | |
text = layout.Text; | |
doc.Close(false); | |
} | |
catch { } | |
finally | |
{ | |
File.Delete(tmpFile); | |
File.Delete(tmpFile + ".bmp"); | |
GC.Collect(); | |
GC.WaitForPendingFinalizers(); | |
} | |
return text; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment