Coffeehouse Thread

6 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

OCR in .NET

Back to Forum: Coffeehouse
  • User profile image
    pathfinder

    Does anyone know of any .NET code or class that will do optical character recognition from an image?

    Specifically I want to take a screen shot and then try to capture some of the text on the screen.    Kind of like a screen scrape. 

    My preference would be something in c# also.    I did a search and came across an open source project called GOCR but I could find any .NET flavors of it.  Do you know of any?

    Thanks.

  • User profile image
    Bas

    I was looking for the same thing a while ago, but all I could find was some commercial library. It might have worked, but I couldn't get the free trial to work.

    Fortunately, the only text I needed to recognize was all in the same font and size, on a white background. So I just grabbed images of all characters and hacked one together myself.

  • User profile image
    pathfinder

    Its a screen scrape of a game, so the text that is rendered will always be the same, but the background will be different.  The text color is pretty consistent so maybe I could extract the color and then do some type of bitmap comparison.

     

  • User profile image
    androidi

    This works but the results atleast with the font I tried to use it on were quite inaccurate. I found that by scaling up the source image it gives better results but still not accurate enough for the font I were dealing with. To overcome this I fortunately were able to use edit distance to pick what was the most likely word from a set of words.

    The OCR interop dll is from here I believe:
    http://www.codeproject.com/office/modi.asp?df=100&forumid=172151&exp=0&select=1775451



    static string AnalyzeImage(string filename)
    {
    MODI.Document doc = new Document();
    doc.Create(filename);
    doc.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, false, false);
    //doc.Save();
    MODI.Image img = (MODI.Image)doc.Images[0];
    return img.Layout.Text;
    }
     
     
    static void CropImageAndSaveAsDestPNG(string fullname)
    {
    Rectangle croparea = new Rectangle(137, 24, 200, 20);
    Bitmap image = new Bitmap(fullname);
    Bitmap b = image.Clone(croparea, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
    Bitmap r = ScaleByPercent(b, 500);
    r.Save("dest.png", System.Drawing.Imaging.ImageFormat.Png);
    image.Dispose(); b.Dispose(); r.Dispose();
    }
     
    static Bitmap ScaleByPercent(Bitmap imgPhoto, int Percent)
    {
    float nPercent = ((float)Percent / 100);
    int sourceWidth = imgPhoto.Width;
    int sourceHeight = imgPhoto.Height;
    int sourceX = 0; int sourceY = 0;
    int destX = 0; int destY = 0;
    int destWidth = (int)(sourceWidth * nPercent);
    int destHeight = (int)(sourceHeight * nPercent);
    Bitmap bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
    bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
    Graphics grPhoto = Graphics.FromImage(bmPhoto);
    grPhoto.InterpolationMode = InterpolationMode.High;
    grPhoto.DrawImage(imgPhoto, new Rectangle(destX, destY, destWidth, destHeight), new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel);
    grPhoto.Dispose();
    return bmPhoto;
    }

  • User profile image
    cheong

    Although not in .NET, I found the COM+ library of MODI(Microsoft Office Document Imaging) works well provided the resolution is not too small and the background is not too colorful. Smiley

    If it's inhouse application, I think that'd be quite enough.

    Recent Achievement unlocked: Code Avenger Tier 4/6: You see dead program. A lot!
    Last modified
  • User profile image
    pathfinder

    I think I may have found a solution.  Although it might take a fair amount of work.  Combine the following two projects

    http://www.codeproject.com/useritems/Managed_PGM.asp

    http://jocr.sourceforge.net/


    and also figure out how to do a screen capture in .NET

    I am suprised that there isn't already a solution out in the wild.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.