1

It is about TWAINdotNet library. I have spent last 24 hours trying to do basic virtual scanner experiment in C#/WPF/VS2010. I have already downloaded and installed virtual scanner driver from http://sourceforge.net/projects/twain-samples/files/ and it appears just fine in the list of available TWAIN data sources. Interestingly, the sample application that is available on this page works perfectly fine with this driver too. The problem occurs only when I try to use it through C#/WPF.

Specifically, calling DsUserInterface using Message.EnableDS fails for God-knows what reason. It fails in all cases whether I ask it to show UI or not. Similarly DsImageLayout call also results in Failure when trying to set the scan area. Last but not the least, trying to set measurement units to inches (or whatever) also doesn't take effect. The call doesn't fail, but the previous value remains intact.

Here's the code in case I may be doing something wrong:

public partial class MainWindow : Window
{
    TwainDotNet.Wpf.WpfWindowMessageHook mTwnHook;
    TwainDotNet.Twain mTWN;
    TwainDotNet.ScanSettings setting = new TwainDotNet.ScanSettings();

    public MainWindow()
    {
        InitializeComponent();
    }

    private void btnScan_Click(object sender, RoutedEventArgs e)
    {
        InitializeTWAIN();

        mTWN.ScanningComplete += twn_ScanningComplete;
        mTWN.TransferImage += twn_TransferImage;

        mTWN.StartScanning(setting);            
    }

    private void InitializeTWAIN()
    {
        mTwnHook = new TwainDotNet.Wpf.WpfWindowMessageHook(this);
        mTWN = new TwainDotNet.Twain(mTwnHook);

        setting.UseAutoFeeder = true;
        setting.UseDocumentFeeder = true;
        setting.UseDuplex = true;
        setting.TransferCount = 1;

        setting.Page = new TwainDotNet.PageSettings() 
        { 
                    Orientation = TwainDotNet.TwainNative.Orientation.Auto, 
                    Size = TwainDotNet.TwainNative.PageType.UsLetter 
        };

        setting.Area = new TwainDotNet.AreaSettings(TwainDotNet.TwainNative.Units.Millimeters, 0f, 0f, 279.4f, 215.9f);
        setting.Resolution = new TwainDotNet.ResolutionSettings() 
        { 
                    ColourSetting = TwainDotNet.ColourSetting.Colour, 
                    Dpi = 100 
        };

        setting.ShouldTransferAllPages = true;
        setting.ShowTwainUI = false;
    }

    void twn_TransferImage(object sender, TwainDotNet.TransferImageEventArgs e)
    {
      //save  image
    }

    void twn_ScanningComplete(object sender, TwainDotNet.ScanningCompleteEventArgs e)
    {
        if (e.Exception != null)
            MessageBox.Show(e.Exception.Message);
    }
}
Jørgen R
  • 10,568
  • 7
  • 42
  • 59
dotNET
  • 33,414
  • 24
  • 162
  • 251

0 Answers0