/// <summary> /// Handles the UnhandledException event of the AppDomain.CurrentDomain. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="UnhandledExceptionEventArgs"/> instance containing the event data.</param> private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { System.ComponentModel.LicenseException licenseException = GetLicenseException(e.ExceptionObject); if (licenseException != null) { // show information about licensing exception MessageBox.Show(string.Format("{0}: {1}", licenseException.GetType().Name, licenseException.Message), "Error", MessageBoxButton.OK, MessageBoxImage.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(); } }
/// <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; } }