private void ChooseDevice(string driverName) { var driver = driverFactory.Create(driverName); try { driver.DialogParent = this; ScanDevice device = driver.PromptForDevice(); if (device != null) { if (string.IsNullOrEmpty(txtName.Text) || CurrentDevice != null && CurrentDevice.Name == txtName.Text) { txtName.Text = device.Name; } CurrentDevice = device; } } catch (ScanDriverException e) { if (e is ScanDriverUnknownException) { Log.ErrorException(e.Message, e.InnerException); } errorOutput.DisplayError(e.Message); } }
private async void btnScan_Click(object sender, EventArgs e) { // prepare the scan profile if (defaultScanProfile == null) { defaultScanProfile = new ScanProfile { Version = ScanProfile.CURRENT_VERSION }; defaultScanProfile.DriverName = "twain"; defaultScanParams = new ScanParams(); var driver = driverFactory.Create(defaultScanProfile.DriverName); driver.ScanProfile = defaultScanProfile; driver.ScanParams = defaultScanParams; var deviceList = driver.GetDeviceList(); if (!deviceList.Any()) { MessageBox.Show("There is no connected device!"); return; } defaultScanProfile.Device = deviceList[0]; } // perfor scan do { await scanPerformer.PerformScan(defaultScanProfile, defaultScanParams, this, notify, ReceiveScannedImage()); } while (MessageBox.Show("Would you like to continue?", "Question", MessageBoxButtons.YesNo) == DialogResult.Yes); SavePDF(imageList.Images); imageList.Delete(Enumerable.Range(0, imageList.Images.Count)); }
public List <ScanDevice> GetDeviceList(ScanProfile scanProfile) { if (scanProfile.DriverName == ProxiedScanDriver.DRIVER_NAME) { scanProfile.DriverName = scanProfile.ProxyDriverName; } var driver = scanDriverFactory.Create(scanProfile.DriverName); driver.ScanProfile = scanProfile; return(driver.GetDeviceList()); }
public void PerformScan(ExtendedScanSettings scanSettings, IWin32Window dialogParent, IScanReceiver scanReceiver) { var driver = driverFactory.Create(scanSettings.DriverName); driver.DialogParent = dialogParent; driver.ScanSettings = scanSettings; try { if (scanSettings.Device == null) { // The profile has no device specified, so prompt the user to choose one var device = driver.PromptForDevice(); if (device == null) { // User cancelled return; } driver.ScanDevice = device; } else { // The profile has a device specified, so use it driver.ScanDevice = scanSettings.Device; } foreach (IScannedImage scannedImage in driver.Scan()) { scanReceiver.ReceiveScannedImage(scannedImage); Application.DoEvents(); } } catch (ScanDriverException e) { if (e is ScanDriverUnknownException) { Log.ErrorException(e.Message, e.InnerException); } errorOutput.DisplayError(e.Message); } }
public async Task PerformScan(ScanProfile scanProfile, ScanParams scanParams, IWin32Window dialogParent, ISaveNotify notify, Action <ScannedImage> imageCallback, CancellationToken cancelToken = default) { var driver = driverFactory.Create(scanProfile.DriverName); driver.DialogParent = dialogParent; driver.ScanProfile = scanProfile; driver.ScanParams = scanParams; driver.CancelToken = cancelToken; try { if (scanProfile.Device == null) { // The profile has no device specified, so prompt the user to choose one var device = driver.PromptForDevice(); if (device == null) { // User cancelled return; } if (appConfigManager.Config.AlwaysRememberDevice) { scanProfile.Device = device; profileManager.Save(); } driver.ScanDevice = device; } else { // The profile has a device specified, so use it driver.ScanDevice = scanProfile.Device; } // Start the scan int imageCount = 0; var source = driver.Scan().Then(img => imageCount++); bool doAutoSave = !scanParams.NoAutoSave && !appConfigManager.Config.DisableAutoSave && scanProfile.EnableAutoSave && scanProfile.AutoSaveSettings != null; if (doAutoSave) { if (scanProfile.AutoSaveSettings.ClearImagesAfterSaving) { // Auto save without piping images var images = await source.ToList(); if (await autoSave.Save(scanProfile.AutoSaveSettings, images, notify)) { foreach (ScannedImage img in images) { img.Dispose(); } } else { // Fallback in case auto save failed; pipe all the images back at once foreach (ScannedImage img in images) { imageCallback(img); } } } else { // Basic auto save, so keep track of images as we pipe them and try to auto save afterwards var images = new List <ScannedImage>(); await source.ForEach(scannedImage => { imageCallback(scannedImage); images.Add(scannedImage); }); await autoSave.Save(scanProfile.AutoSaveSettings, images, notify); } } else { // No auto save, so just pipe images back as we get them await source.ForEach(imageCallback); } if (imageCount > 0) { Log.Event(EventType.Scan, new Event { Name = MiscResources.Scan, Pages = imageCount, DeviceName = scanProfile.Device?.Name, ProfileName = scanProfile.DisplayName, BitDepth = scanProfile.BitDepth.Description() }); } } catch (ScanDriverException e) { if (e is ScanDriverUnknownException) { Log.ErrorException(e.Message, e.InnerException); errorOutput.DisplayError(e.Message, e); } else { errorOutput.DisplayError(e.Message); } } }
public void PerformScan(ScanProfile scanProfile, ScanParams scanParams, IWin32Window dialogParent, ISaveNotify notify, Action <ScannedImage> imageCallback) { var driver = driverFactory.Create(scanProfile.DriverName); driver.DialogParent = dialogParent; driver.ScanProfile = scanProfile; driver.ScanParams = scanParams; try { if (scanProfile.Device == null) { // The profile has no device specified, so prompt the user to choose one var device = driver.PromptForDevice(); if (device == null) { // User cancelled return; } if (appConfigManager.Config.AlwaysRememberDevice) { scanProfile.Device = device; profileManager.Save(); } driver.ScanDevice = device; } else { // The profile has a device specified, so use it driver.ScanDevice = scanProfile.Device; } bool doAutoSave = !scanParams.NoAutoSave && !appConfigManager.Config.DisableAutoSave && scanProfile.EnableAutoSave && scanProfile.AutoSaveSettings != null; if (doAutoSave) { if (scanProfile.AutoSaveSettings.ClearImagesAfterSaving) { // Auto save without piping images var images = driver.Scan().ToList(); if (autoSave.Save(scanProfile.AutoSaveSettings, images, notify)) { foreach (ScannedImage img in images) { img.Dispose(); } } else { // Fallback in case auto save failed; pipe all the images back at once foreach (ScannedImage img in images) { imageCallback(img); } } } else { // Basic auto save, so keep track of images as we pipe them and try to auto save afterwards var images = new List <ScannedImage>(); foreach (ScannedImage scannedImage in driver.Scan()) { imageCallback(scannedImage); images.Add(scannedImage); } autoSave.Save(scanProfile.AutoSaveSettings, images, notify); } } else { // No auto save, so just pipe images back as we get them foreach (ScannedImage scannedImage in driver.Scan()) { imageCallback(scannedImage); } } } catch (ScanDriverException e) { if (e is ScanDriverUnknownException) { Log.ErrorException(e.Message, e.InnerException); errorOutput.DisplayError(e.Message, e); } else { errorOutput.DisplayError(e.Message); } } }