Send comments on this topic. | Back to Introduction - All Topics | Help Version 16.5.9.25
OCR an Image

Take the following steps to create an ASP.NET AJAX-Enabled Web Application to OCR an image. The pre-requisite for this tutorial is that you have LEADTOOLS WCF OcrService hosted.

For more information on hosting the WCF services see: How to Host LEADTOOLS Services on IIS 7 or How to Host LEADTOOLS Services on IIS 5 and 6.

  1. Choose File->New->Project... from the menu.
  2. In the New Project dialog box, choose either "Visual C# Projects" or "Visual Basic Projects" in the Projects Type List, and choose "ASP.NET AJAX-Enabled Web Application" in the Templates List.
  3. Type the project name as "OcrServiceExample" in the Project Name field, and then choose OK. If desired, type a new location for your project or select a directory using the Browse button, and then choose OK.
  4. In the "Solution Explorer" window, right-click on the "References" folder, and select "Add Service Reference" from the context menu. In the "Add Reference" dialog box, enter the URI for the OcrService.svc in the "Service Uri" field, and enter "OcrService" for the "Service reference name" field.
  5. Double-click on Default.aspx in the Solution Explorer. Add the "Update Panel" control from the toolbox. The ScriptManager control should already exist on the page.
  6. Select the "Button" control from the toolbox and place it inside the UpdatePanel.
  7. Select the "Label" control from the toolbox and place it inside the UpdatePanel.
  8. Right-click on "Default.aspx" and select "View Code". Add the following lines to the beginning of the file:

    [Visual Basic]

    Imports AJAXEnabledWebApplication1.OcrService

    [C#]

    using AJAXEnabledWebApplication1.OcrService;

     

  9. Switch to Design view and double click on the Button to generate an event handler. Copy and paste the code below in the Button1_Click event.

    [Visual Basic]

    
    Dim load As New FileBinaryData()
    load.FileName = "D:\Images\Raster\OCR1.tif"
    Dim save As New RawBinaryData()
    ' Set the converstion options.
    Dim opts As New DocumentConvertOptions()
    opts.Format = OcrDocumentFormatType.AsciiText
    opts.Source = load
    opts.Destination = save
    Dim svc As New OcrService.OcrServiceClient()
    save = TryCast(svc.Recognize(opts, OcrEngineType.Plus), RawBinaryData)
    Label1.Text = "<pre>" & System.Text.Encoding.ASCII.GetString(save.Data) & "</pre>"

    [C#]

     

    
    FileBinaryData load = new FileBinaryData();
    load.FileName = @"D:\Images\Raster\OCR1.tif";

    
    RawBinaryData save = new RawBinaryData();

    
    // Set the converstion options.
    DocumentConvertOptions opts = new DocumentConvertOptions();
    opts.Format = OcrDocumentFormatType.AsciiText;
    opts.Source = load;
    opts.Destination = save;

    
    OcrService.OcrServiceClient svc = new OcrService.OcrServiceClient();
    save = svc.Recognize(opts, OcrEngineType.Plus) as RawBinaryData;

    
    Label1.Text = "<pre>" + System.Text.Encoding.ASCII.GetString(save.Data) + "</pre>";

     

  10. Build, and Run the web application to test it.