Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to Scan Documents with your WPF Application

7 Sep 2012 0  
In this article, I’ll share with you how to utilize Dynamic .NET TWAIN in a WPF application to process your documents.

Introduction

Windows Presentation Foundation (WPF), introduced with .NET framework 3.0, is an API for creating graphical user interfaces for the Windows platform. Although WinForm is a mature proven technology, more and more developers turn to WPF for its better handling of Graphics and animation. Compared to the traditional forms applications, WPF allows you to customize controls in a most efficient and flexible way.

Dynamsoft’s Dynamic .NET TWAIN is a .NET component that can be embedded into a WPF application. You can use the component in different stages of document processing, from image scanning, editing to the final image storage. In this article, I’ll provide the integration steps and the image-processing functions/features that can be implemented.

Sample Code

Here I assume you’ve already installed Dynamic .NET TWAIN on your development machine.

  1. Open Visual Studio and create a WPF application.
  2. Go to Solution Explorer -> References, add “Dynamic .NET TWAIN” and any other .NET references that you can’t find in the Explorer as shown in the below code snippet:

  3. Initialize Dynamic .NET TWAIN component.
    1. Create a grid to hold the component. Change the .xaml file like this:
      <Window x:Class="WpfDemo.Window1"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          Title="Acquire Image" Height="600" Width="500" Loaded="Window_Loaded">
          <Grid Name="grid1" Height="400" Width="300">
              <WindowsFormsHost ></WindowsFormsHost>    </Grid>
      </Window>
    2. In the .xaml.cs file, add the namespace for Dynamic .NET TWAIN, and then insert the following lines of code in the event Window_Loaded to initialize the component.
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using Dynamsoft.DotNet.TWAIN;
    
    namespace WpfAppDNT
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
            DynamicDotNetTwain objDynamicDotNetTwain = null;
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                System.Windows.Forms.Integration.WindowsFormsHost host = new System.Windows.Forms.Integration.WindowsFormsHost();
                objDynamicDotNetTwain = new DynamicDotNetTwain();
                objDynamicDotNetTwain.Width = 200;
                objDynamicDotNetTwain.Height = 300;
                host.Child = objDynamicDotNetTwain;
                this.grid1.Children.Add(host);
            }
    }
    }
  4. Go to the .xaml file to add the required buttons. In this sample, you can see that two buttons have been added: btnSelect and btnAcquire. More functions can be added according to your requirements.
    <Window x:Class="WpfAppDNT.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="600" Width="500" Loaded="Window_Loaded">
        <Grid Name="grid1" Height="400" Width="300">
            <WindowsFormsHost></WindowsFormsHost>
            <Button Height="23" HorizontalAlignment="Left" Margin="0,0,0,-37" Name="btnSelect" VerticalAlignment="Bottom" Width="88" Click="btnSelect_Click">Select  Source
            </Button>
            <Button Height="23" Margin="103,0,148,-37" Name="btnAcquire" VerticalAlignment="Bottom" Click="btnAcquire_Click">Acquire</Button>
        </Grid>
    </Window>
  5. Image Scanning. After you add the buttons, you can insert the source code to implement the planned functions. btnSelect is going to show all the available sources of your devices, including attached/remote scanners, webcams, etc. The latest version of Dynamic .NET TWAIN (v4.1) is compatible with all TWAIN, WIA and UVC compatible devices.
            private void btnSelect_Click(object sender, RoutedEventArgs e)
            {
                objDynamicDotNetTwain.SelectSource();
            }

With btnAcquire, you can capture images and documents from the selected device.

        private void btnAcquire_Click(object sender, RoutedEventArgs e)
        {          
            objDynamicDotNetTwain.IfDisableSourceAfterAcquire = true;
            objDynamicDotNetTwain.AcquireImage();
        }

By default, most of the devices (including TWAIN2 FreeImage Software Scanner 2.1 shown in the screenshot) will prompt you the user interface of the source when clicking on "Acquire". Through the user interface, users can adjust the properties of the images, for instance, single/duplex scan, color mode, resolution, page size, brightness, contrast, etc. This gives your customers the most flexibility during the document scanning process. However, some businesses and organizations, such as the banks and governments planning to scan user IDs, may want to standardize the page size, resolution and/or other image properties. This is also supported by Dynamic .NET TWAIN. A rich set of properties are provided by the control that enables you to hard code the properties. Here is an example:

Other Features Supported by Dynamic .NET TWAIN

Besides image scanning, you can add many more functions to your WPF application with the help of Dynamic .NET TWAIN.

  • Edit the scanned the images. Rotate, crop, flip and/or mirror the images to better fit your needs.
  • Zoom in/out the images.
  • Upload the scanned images to the local folder, FTP site, web server or database.

Sample Code Download

If you are interested in the WPF sample, you can download the source code at:

If you’d like to learn more about Dynamic .NET TWAIN, the 30-day free trial is available for you.

Dynamic .NET TWAIN 30-day Free Trial Download
Other demos of Dynamic .NET TWAIN

If you have any questions, you can contact our support team at nettwain@dynamsoft.com.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here