/// <summary>
        /// Application form is closing.
        /// </summary>
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (_currentDevice != null)
            {
                UnsubscribeFromDeviceEvents();
                _currentDevice = null;
            }

            // close the device manager
            _deviceManager.Close();
            // dispose the device manager
            _deviceManager.Dispose();
        }
Example #2
0
        /// <summary>
        /// Scans images.
        /// </summary>
        private void scanImagesButton_Click(object sender, EventArgs e)
        {
            try
            {
                // disable application UI
                scanImagesButton.Enabled = false;

                // create TWAIN device manager
                using (DeviceManager deviceManager = new DeviceManager(this, this.Handle))
                {
                    try
                    {
                        // try to find TWAIN device manager
                        deviceManager.IsTwain2Compatible = twain2CheckBox.Checked;
                    }
                    catch (Exception ex)
                    {
                        // show dialog with error message
                        MessageBox.Show(GetFullExceptionMessage(ex), "TWAIN device manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    // if 64-bit TWAIN2 device manager is used
                    if (IntPtr.Size == 8 && deviceManager.IsTwain2Compatible)
                    {
                        if (!InitTwain2DeviceManagerMode(deviceManager))
                        {
                            return;
                        }
                    }

                    try
                    {
                        // open the device manager
                        deviceManager.Open();
                    }
                    catch (Exception ex)
                    {
                        // show dialog with error message
                        MessageBox.Show(GetFullExceptionMessage(ex), "TWAIN device manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    // if devices are NOT found
                    if (deviceManager.Devices.Count == 0)
                    {
                        MessageBox.Show("Devices are not found.");
                        return;
                    }

                    // if device is NOT selected
                    if (!deviceManager.ShowDefaultDeviceSelectionDialog())
                    {
                        MessageBox.Show("Device is not selected.");
                        return;
                    }

                    // get reference to the selected device
                    _device = deviceManager.DefaultDevice;

                    // set scan settings
                    _device.ShowUI                 = showUiCheckBox.Checked;
                    _device.ShowIndicators         = showIndicatorsCheckBox.Checked;
                    _device.DisableAfterAcquire    = !_device.ShowUI;
                    _device.CloseAfterModalAcquire = false;

                    AcquireModalState acquireModalState;
                    do
                    {
                        // synchronously acquire image from device
                        acquireModalState = _device.AcquireModal();
                        switch (acquireModalState)
                        {
                        case AcquireModalState.ImageAcquired:
                            // dispose previous bitmap in the picture box
                            if (pictureBox1.Image != null)
                            {
                                pictureBox1.Image.Dispose();
                                pictureBox1.Image = null;
                            }

                            // set a bitmap in the picture box
                            pictureBox1.Image = _device.AcquiredImage.GetAsBitmap(true);

                            // dispose an acquired image
                            _device.AcquiredImage.Dispose();
                            break;

                        case AcquireModalState.ScanCanceled:
                            MessageBox.Show("Scan is canceled.");
                            break;

                        case AcquireModalState.ScanFailed:
                            MessageBox.Show(string.Format("Scan is failed: {0}", _device.ErrorString));
                            break;
                        }
                    }while (acquireModalState != AcquireModalState.None);

                    // close the device
                    _device.Close();
                    _device = null;

                    // close the device manager
                    deviceManager.Close();
                }
            }
            catch (TwainException ex)
            {
                MessageBox.Show(GetFullExceptionMessage(ex));
            }
            catch (Exception ex)
            {
                System.ComponentModel.LicenseException licenseException = GetLicenseException(ex);
                if (licenseException != null)
                {
                    // show information about licensing exception
                    MessageBox.Show(string.Format("{0}: {1}", licenseException.GetType().Name, licenseException.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    // open article with information about usage of evaluation license
                    System.Diagnostics.Process process = new System.Diagnostics.Process();
                    process.StartInfo.FileName        = "https://www.vintasoft.com/docs/vstwain-dotnet/Licensing-Twain-Evaluation.html";
                    process.StartInfo.UseShellExecute = true;
                    process.Start();
                }
                else
                {
                    throw;
                }
            }
            finally
            {
                // enable application UI
                scanImagesButton.Enabled = true;
            }
        }
        /// <summary>
        /// Opens TWAIN device manager.
        /// </summary>
        private void openDeviceManagerButton_Click(object sender, RoutedEventArgs e)
        {
            // clear list of devices
            devicesComboBox.Items.Clear();

            // if device manager is opened
            if (_deviceManager.State == DeviceManagerState.Opened)
            {
                // close the device manager
                _deviceManager.Close();

                // change text on this button
                openDeviceManagerButton.Content = "Open device manager";
            }

            // if device manager is closed
            else
            {
                try
                {
                    // try to find the device manager specified by user
                    if (twain2CompatibleCheckBox.IsChecked == true)
                    {
                        _deviceManager.IsTwain2Compatible = true;
                    }
                    else
                    {
                        _deviceManager.IsTwain2Compatible = false;
                    }
                }
                catch (Exception ex)
                {
                    // show dialog with error message
                    MessageBox.Show(GetFullExceptionMessage(ex), "TWAIN device manager", MessageBoxButton.OK, MessageBoxImage.Error);

                    // open a HTML page with article describing how to solve the problem
                    OpenBrowser("https://www.vintasoft.com/docs/vstwain-dotnet/Programming-Twain-Device_Manager.html");

                    return;
                }

                // device manager is found

                // if 64-bit TWAIN2 device manager is used
                if (IntPtr.Size == 8 && _deviceManager.IsTwain2Compatible)
                {
                    if (!InitTwain2DeviceManagerMode())
                    {
                        return;
                    }
                }

                try
                {
                    // open the device manager
                    _deviceManager.Open();
                }
                catch (TwainDeviceManagerException ex)
                {
                    // close the device manager
                    _deviceManager.Close();

                    // show dialog with error message
                    MessageBox.Show(GetFullExceptionMessage(ex), "TWAIN device manager", MessageBoxButton.OK, MessageBoxImage.Error);

                    // open a HTML page with article describing how to solve the problem
                    OpenBrowser("http://www.vintasoft.com/docs/vstwain-dotnet/index.html?Programming-Twain-Device_Manager.html");

                    return;
                }

                DeviceCollection devices = _deviceManager.Devices;
                // for each available device
                for (int i = 0; i < devices.Count; i++)
                {
                    // add the device name to a combo box
                    devicesComboBox.Items.Add(devices[i].Info.ProductName);

                    // if device is default device
                    if (devices[i] == _deviceManager.DefaultDevice)
                    {
                        // select device in a combo box
                        devicesComboBox.SelectedIndex = i;
                    }
                }

                // change text on this button
                openDeviceManagerButton.Content = "Close device manager";
            }

            // update UI
            UpdateUI();
        }
Example #4
0
        /// <summary>
        /// Selects the device and acquires images from device.
        /// </summary>
        public void SelectDeviceAndAcquireImage()
        {
#if !REMOVE_TWAIN_SDK
#if NETCORE
            DeviceManager deviceManager = new DeviceManager(_parentForm, _parentForm.Handle);
#else
            DeviceManager deviceManager = new DeviceManager(_parentForm);
#endif
            try
            {
                Device device = null;
                try
                {
                    // try to use TWAIN device manager 2.x
                    deviceManager.IsTwain2Compatible = true;
                    // if TWAIN device manager 2.x is not found
                    if (!deviceManager.IsTwainAvailable)
                    {
                        try
                        {
                            // try to use TWAIN device manager 1.x
                            deviceManager.IsTwain2Compatible = false;
                        }
                        catch
                        {
                            if (IntPtr.Size != 8)
                            {
                                throw;
                            }
                        }
                        // if TWAIN device manager 1.x is not found
                        if (!deviceManager.IsTwainAvailable)
                        {
                            // show dialog with error message
                            MessageBox.Show("TWAIN device manager is not found.", "TWAIN device manager", MessageBoxButtons.OK, MessageBoxIcon.Error);

                            // open a HTML page with article describing how to solve the problem
                            DemosTools.OpenBrowser("https://www.vintasoft.com/docs/vstwain-dotnet/Programming-Twain-Device_Manager.html");
                            return;
                        }
                    }

                    // if 64-bit TWAIN2 device manager is used
                    if (IntPtr.Size == 8 && deviceManager.IsTwain2Compatible)
                    {
                        if (!InitTwain2DeviceManagerMode(deviceManager))
                        {
                            return;
                        }
                    }

                    try
                    {
                        // open the device manager
                        deviceManager.Open();
                    }
                    catch (Exception ex)
                    {
                        // show dialog with error message
                        DemosTools.ShowErrorMessage("TWAIN device manager", ex);
                        return;
                    }

                    // if devices are not found in the system
                    if (deviceManager.Devices.Count == 0)
                    {
                        MessageBox.Show("Devices are not found.");
                        return;
                    }

                    // if device is not selected
                    if (!deviceManager.ShowDefaultDeviceSelectionDialog())
                    {
                        return;
                    }

                    // get reference to the current device
                    device = deviceManager.DefaultDevice;

                    device.ShowUI = true;

                    // acquire image(s) from the device
                    AcquireModalState acquireModalState = AcquireModalState.None;
                    do
                    {
                        acquireModalState = device.AcquireModal();
                        switch (acquireModalState)
                        {
                        case AcquireModalState.ImageAcquired:
                            // get acquired image as Bitmap and add Bitmap to the image collection
                            _images.Add(new VintasoftImage(device.AcquiredImage.GetAsBitmap(), true));
                            // dispose the acquired image
                            device.AcquiredImage.Dispose();
                            break;

                        case AcquireModalState.ScanCompleted:
                            break;

                        case AcquireModalState.ScanCanceled:
                            MessageBox.Show("Scan canceled.");
                            break;

                        case AcquireModalState.ScanFailed:
                            MessageBox.Show(string.Format("Scan failed: {0}", device.ErrorString));
                            break;
                        }
                    }while (acquireModalState != AcquireModalState.None);
                }
                catch (TwainInvalidStateException ex)
                {
                    DemosTools.ShowErrorMessage(ex);
                }
                catch (TwainDeviceManagerException ex)
                {
                    DemosTools.ShowErrorMessage(ex);
                }
                catch (TwainDeviceException ex)
                {
                    DemosTools.ShowErrorMessage(ex);
                }
                finally
                {
                    if (device != null && device.State == DeviceState.Opened)
                    {
                        // close the device
                        device.Close();
                    }

                    if (deviceManager.State != DeviceManagerState.Closed)
                    {
                        // close the device manager
                        deviceManager.Close();
                    }
                }
            }
            finally
            {
                deviceManager.Dispose();
            }
#endif
        }