Click here to Skip to main content

OCR using Silverlight 5


It was impossible to implement the OCR module of Document Management System which is capable of scanning the local documents( Eg: passport) of user and extract the text , in ASP.NET alone. So I implemented an RIA(Rich Internet Application) using Silverlight, WCF, MODI (Microsoft Office Document Imaging) . I would like to share the idea for who seeking the same.

If you are going to implement the exact MRZ application, take a look at along with this article



  • Visual studio 2010-SP1
  • Silverlight 5 developer tools
  • MS Office 2007

By default MODI is disabled in MS Office. Please enable it from

  1. Add or remove programms-> Microsoft Office 2007->Change
  2. Add or remove features-> Office tools
  3. Microsoft office document imaging-> Scanning, OCR and Indexing service filter .

To make your application possible to access scanner device of client machine from browser, your application must be 'trusted one'. To make the application trusted, follow these

Application must be 'Runnable out of browser'

  • Go to properties of silverlight application and check the properties.

  • Edit the registry using regedit.exe in client machine
  • Sign the .xap file

To enable in-browser trusted applications

  1. Configure the target computers to allow trusted applications inside the browser by setting the following registry key:
    • Key path for 32-bit computers: HKEY_LOCAL_MACHINE\Software\Microsoft\Silverlight\
    • Key path for 64-bit computers: HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Silverlight\
    • Value name: AllowElevatedTrustAppsInBrowser
    • Value type: DWORD
    • Valid Values:
      • Disabled - 0x00000000
      • Enabled - 0x00000001
  2. Ensure that you do not set conflicting registry keys. For more information, see the AllowInstallOfElevatedTrustApps and AllowLaunchOfElevatedTrustApps registry keys in Group Policy Settings. Specifically, you can disallow the installation of trusted applications without affecting their in-browser use. However, disallowing the launch of trusted applications will cause them to run in partial trust, even inside the browser.
  3. Sign the .xap files with a valid, code-signing certificate. For more information, see the "Application Signing" section of Trusted Applications.
  4. Install the certificate to the Trusted Publishers certificate store and (if necessary) the Trusted Root Certification Authorities store. For more information, see Deploying Certificates to the Trusted Publishers Store.

    Visit for more detals:

Using the code

The web.config file of host application is edited to increase the buffer size.

<?xml version="1.0"?>
  For more information on how to configure your ASP.NET application, please visit
    <compilation debug="true" targetFramework="4.0" />
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        <binding name ="ServicesBinding" 
             maxReceivedMessageSize="2147483647" maxBufferSize="2147483647">
          <readerQuotas maxArrayLength ="2147483647" maxBytesPerRead="2147483647" 
            maxDepth ="2147483647" maxNameTableCharCount ="2147483647" 
            maxStringContentLength ="2147483647"/>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
        multipleSiteBindingsEnabled="true" />
      <service name="SilverlightApplication1.Web.Service1">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="ServicesBinding"
            contract="SilverlightApplication1.Web.Service1" >
            <dns value ="localhost"/>
        </endpoint >
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />

Acquire image from scanner in silverlight page (Not that, this is unmanaged code)

using (dynamic CommonDialog = AutomationFactory.CreateObject("WIA.CommonDialog"))
        dynamic imageFile = CommonDialog.ShowAcquireImage(
      "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}", false, true, false);}

The following WCF method extracts text from image using MODI

public String doOCR(byte[] image)
    FileStream fileStream = null;
    BinaryWriter writer = null;
    string filePath;
        filePath = HttpContext.Current.Server.MapPath(@"Images\temp.jpg");             
        MemoryStream ms = new MemoryStream(image);
        Image im = Image.FromStream(ms);
        MODI.Document md = new MODI.Document();
        md.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true);
        MODI.Image img = (MODI.Image)md.Images[0];            
        return img.Layout.Text;                
    catch (Exception ex)
        return ex.InnerException.ToString();
        if (fileStream != null)
        if (writer != null)

Points of Interest

I really enjoy to share what I learned from others.

Web02 | 2.8.160208.1 | Advertise | Privacy
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service