Click here to Skip to main content
15,867,686 members
Articles / Web Development / ASP.NET
Article

How to Scan Documents with your WPF Application

7 Sep 2012CPOL3 min read 46.5K   2K   15   4
In this article, I’ll share with you how to utilize Dynamic .NET TWAIN in a WPF application to process your documents.

This article is in the Product Showcase section for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers.

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:

    Image 1

  3. Initialize Dynamic .NET TWAIN component.
    1. Create a grid to hold the component. Change the .xaml file like this:
      XML
      <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.
    C#
    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.
    C#
    private void btnSelect_Click(object sender, RoutedEventArgs e)
    {
        objDynamicDotNetTwain.SelectSource();
    }
    

    Image 2

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

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

Image 3

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Canada Canada
Dynamsoft has more than 15 years of experience in TWAIN SDKs, imaging SDKs and version control solutions.

Our products include:

TWAIN SDK
- Dynamic Web TWAIN: a TWAIN scanning SDK optimized for web document management applications.
- Dynamic .NET TWAIN: a .NET TWAIN and Directshow Image Capture SDK for WinForms/WPF applications.

Imaging SDKs
- Barcode Reader for Windows, Linux, macOS, iOS, Android and Raspberry Pi.
- OCR addon for both web and .NET TWAIN SDKs

Version Control
- SourceAnywhere: a SQL server-based source control solution. Both on-premise and hosting options are provided.

http://www.dynamsoft.com/
This is a Organisation

21 members

Comments and Discussions

 
GeneralMy vote of 5 Pin
tomzjukent11-Sep-12 17:17
tomzjukent11-Sep-12 17:17 
QuestionGreat article Pin
tomzjukent11-Sep-12 17:14
tomzjukent11-Sep-12 17:14 
GeneralMy vote of 5 Pin
Logan_Ellen11-Sep-12 17:09
Logan_Ellen11-Sep-12 17:09 
I am developing WPF app. This article helps me integrate scanning feature. Thanks
GeneralMy vote of 5 Pin
ShawSdasd11-Sep-12 16:57
ShawSdasd11-Sep-12 16:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.