yshige / twaindotnet

Automatically exported from code.google.com/p/twaindotnet
0 stars 0 forks source link

BackgroundWorker Issue #9

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Call StartScanning from BackgroundWorker:
AutoResetEvent waitHandle = new AutoResetEvent(false);
EventHandler scanCompleteHandler = (se, ev) => { waitHandle.Set(); };
twain.ScanningComplete += scanCompleteHandler;
twain.StartScanning(settings);
waitHandle.WaitOne();
2. Return handle using Dispatcher.Invoke(new Func<IntPtr>(() => 
_interopHelper.Handle)) to avoid cross thread call error in 
WpfWindowMessageHook
3. Access Twain.Images collection
if (twain.Images.Count > 0)
{
    foreach (var image in twain.Images)
    {
        BitmapSource bitmapSource = 
System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(new Bitmap
(image).GetHbitmap(),
            IntPtr.Zero, Int32Rect.Empty, 
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
        bitmapSources.Add(bitmapSource);
    }
}

What is the expected output? What do you see instead?

Scanned images should be available as BitmapSource to bind in Image control

What version of the product are you using? On what operating system?

Latest checkout from the Source control. OS: Windows XP

Please provide any additional information below.

Please find the sample solution attached with this issue

Original issue reported on code.google.com by baburaj....@gmail.com on 12 Feb 2010 at 6:53

Attachments:

GoogleCodeExporter commented 9 years ago
I can reproduce the problem with the code you provided. Since TWAIN is quite 
tied in 
with the Windows messaging system I'm not sure how to fix this. My initial 
thoughts are 
to create a second hidden window and pass that as the parameter to the 
WpfWindowMessageHook.

Original comment by luke.qui...@gmail.com on 21 Feb 2010 at 11:18

GoogleCodeExporter commented 9 years ago
This is the solution that I have found. Sometimes the TestAppWPF application 
hangs on "Transferring data" because in some scanners (with old versions of 
TWAIN) the windows hook is not released after the process is finished:

Creating a second hidden screen does the trick:

public IScannerChildWindow ScannerChildWindow
        {
            get { return _scannerChildWindow; }
            set
            {
                _scannerChildWindow = value;
                if (_scannerChildWindow != null)
                {
                    // This is a twick to make the TWAIN scanner end the windows hook
                    // Adding this windows object in this block will ensure that the GC will eliminate it once the TWAIN scanner
                    // releases the hook
                    Window windows = new Window()
                    {
                        Width = 0,
                        Height = 0,
                        WindowStyle = WindowStyle.None,
                        ShowInTaskbar = false,
                        ShowActivated = false,
                        WindowState = WindowState.Minimized
                    };
                    windows.Show();
                    windows.Hide();

                    _twain = new Twain(new WpfWindowMessageHook(windows));
                    _twain.TransferImage += delegate(object sender, TransferImageEventArgs args)
                    {
                        if (args.Image != null)
                        {

                            this.ScannedImages.Add(Imaging.CreateBitmapSourceFromHBitmap(
                                     new System.Drawing.Bitmap(args.Image).GetHbitmap(),
                                     IntPtr.Zero,
                                     Int32Rect.Empty,
                                     BitmapSizeOptions.FromEmptyOptions()));
                        }
                    };

                    ScannerSourceList = new ObservableCollection<string>(_twain.SourceNames);

                    if (ScannerSourceList != null && ScannerSourceList.Count > 0)
                    {
                        ScannerSourceSelected = ScannerSourceList[0];
                    }
                }
            }
        }

Original comment by Marzan1...@gmail.com on 13 Jul 2012 at 12:25