NTwain datasource open

324 Views Asked by At

The code below is working fine in a console application, when I run the same code in a WCF, it stuck on the datasource open method:

      // Create appId
            Version ver = new Version();
            var appId = TWIdentity.Create(DataGroups.Image | DataGroups.Control, ver, "bla bla", "bla", "bla", "");

            // Attach
            var session = new TwainSession(appId);

            List<Image> scannedImages = new List<Image>();
            session.TransferReady += (s, e) =>
            {
                Debug.Print("TransferReady is a go.");
            };
            session.DataTransferred += (s, e) =>
            {
                if (e.NativeData != IntPtr.Zero)
                {
                    // Handle image data
                    if (e.NativeData != IntPtr.Zero)
                    {
                        var stream = e.GetNativeImageStream();
                        if (stream != null)
                        {
                            //Save Image to list
                            scannedImages.Add(Image.FromStream(stream));
                        }
                    }
                }
            };

            // Open it
            session.Open();

            DataSource myDS = session.FirstOrDefault();
            myDS.Open();
            
            // Start Scan
            myDS.Enable(SourceEnableMode.NoUI, false, IntPtr.Zero);

            //Close Session
            myDS.Close();

I also tried to disable the firewall with no luck. No errors are shown, just infinite loading. Any suggestions?

1

There are 1 best solutions below

1
pwnint On

Maybe you can use TwainSession.ShowSourceSelect() to show twaindsm's selectdialog? I use this method in WinForms. Then you can check that the datasource is null to set the default source, which one dialog last selected source, and just use twainSession.DefaultSource. I think you can close datasource in the SouceDisabled Event, but the TwainSession Never need to close it. You can close it when your application exited or is exiting.