Skip to content

Instantly share code, notes, and snippets.

@tufanbarisyildirim
Created April 10, 2013 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tufanbarisyildirim/5354898 to your computer and use it in GitHub Desktop.
Save tufanbarisyildirim/5354898 to your computer and use it in GitHub Desktop.
OCR Extension metod for System.Drawing.Image using MODI
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