Scanning a document in C# with an Epson DS-30

Bit of a random one today – extremely last minute into one of my projects I have been asked to integrate a consumer grade Epson DS-30.

The first problem, the scanner needs to be controlled with one click – which is actually done by a remote user (that’s a whole other story – and the solution has already been resolved for that).  However after reading the specs of the device – it is clear that there is no such feature available as a standard offering for this hardware.

Luckily I have stumbled upon a great Twain library Saraff.Twain.Net (https://sarafftwain.codeplex.com) which has allowed me to achieve what I needed to do.

The library came with some great samples, and included a method for printing without any UI prompts that the user would have to interact with.

The following gives an example of using the Saraff.Twain Library:


using System;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
using Saraff.Twain;

namespace AutoScan
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private static void twain32_EndXfer(object sender, Twain32.EndXferEventArgs e)
        {
            try
            {
                var file = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),
                    Path.ChangeExtension(Path.GetFileName(Path.GetTempFileName()), ".jpg"));
                e.Image.Save(file, ImageFormat.Jpeg);
                e.Image.Dispose();
            }
            catch (Exception)
            {
                //We had an issue saving the file
            }
        }

        private static void twain32_AcquireError(object sender, Twain32.AcquireErrorEventArgs e)
        {
            MessageBox.Show(@"Acquire Failed.");
        }

        private static void twain32_AcquireCompleted(object sender, EventArgs e)
        {
            MessageBox.Show(@"Acquire Completed.");
        }

        private void btnScan_Click(object sender, EventArgs e)
        {
            try
            {
                using (var twain32 = new Twain32())
                {
                    twain32.ShowUI = false;
                    twain32.IsTwain2Enable = false;

                    twain32.OpenDSM();

                    twain32.SourceIndex = Convert.ToInt32(2);

                    twain32.OpenDataSource();

                    if (!twain32.ShowUI)
                    {
                        twain32.Capabilities.XResolution.Set(150);
                        twain32.Capabilities.YResolution.Set(150);
                    }


                    twain32.Capabilities.PixelType.Set(TwPixelType.RGB);


                    twain32.EndXfer += twain32_EndXfer;

                    twain32.AcquireCompleted += twain32_AcquireCompleted;
                    twain32.AcquireError += twain32_AcquireError;


                    twain32.Acquire();
                }
            }
            catch (Exception)
            {
                //It went wrong!
            }
        }
    }
}

The coding above isn’t ideal as I have had to resort to hard coding the scanner etc… however for the project I am working on the hardware wont be changing.

Happy Days