public static ThrowDeviceError ( |
||
e | ||
return | void |
private Stream DoTransfer(int pageNumber, WiaBackgroundEventLoop eventLoop, string format) { var invoker = (FormBase)DialogParent; if (eventLoop.GetSync(wia => wia.Item) == null) { return(null); } if (ScanParams.NoUI) { return(eventLoop.GetSync(wia => WiaApi.Transfer(wia, format, false))); } if (pageNumber == 1) { // The only downside of the common dialog is that it steals focus. // If this is the first page, then the user has just pressed the scan button, so that's not // an issue and we can use it and get the benefits of progress display and immediate cancellation. return(ScanParams.Modal ? eventLoop.GetSync(wia => invoker.InvokeGet(() => WiaApi.Transfer(wia, format, true))) : eventLoop.GetSync(wia => WiaApi.Transfer(wia, format, true))); } // For subsequent pages, we don't want to take focus in case the user has switched applications, // so we use the custom form. var form = formFactory.Create <FScanProgress>(); var waitHandle = new AutoResetEvent(false); form.PageNumber = pageNumber; form.Transfer = () => eventLoop.GetSync(wia => WiaApi.Transfer(wia, format, false)); form.Closed += (sender, args) => waitHandle.Set(); if (ScanParams.Modal) { invoker.Invoke(() => form.ShowDialog(DialogParent)); } else { invoker.Invoke(() => form.Show(DialogParent)); } waitHandle.WaitOne(); if (form.Exception != null) { WiaApi.ThrowDeviceError(form.Exception); } if (form.DialogResult == DialogResult.Cancel) { return(null); } return(form.ImageStream); }
public Stream Transfer(int pageNumber, WiaBackgroundEventLoop eventLoop, string format) { if (eventLoop.GetSync(wia => wia.Item) == null) { return(null); } if (pageNumber == 1) { // The only downside of the common dialog is that it steals focus. // If this is the first page, then the user has just pressed the scan button, so that's not // an issue and we can use it and get the benefits of progress display and immediate cancellation. ImageFile imageFile = eventLoop.GetSync(wia => (ImageFile) new CommonDialogClass().ShowTransfer(wia.Item, format)); if (imageFile == null) { return(null); } return(new MemoryStream((byte[])imageFile.FileData.get_BinaryData())); } // For subsequent pages, we don't want to take focus in case the user has switched applications, // so we use the custom form. var form = formFactory.Create <FScanProgress>(); form.PageNumber = pageNumber; form.EventLoop = eventLoop; form.Format = format; form.ShowDialog(); if (form.Exception != null) { WiaApi.ThrowDeviceError(form.Exception); } if (form.DialogResult == DialogResult.Cancel) { return(null); } return(form.ImageStream); }
public void DoSync(Action <WiaState> action) { Exception error = null; form.Invoke(new Action(() => { try { if (wiaState == null) { wiaState = InitWia(); } action(wiaState); } catch (Exception ex) { error = ex; } })); if (error != null) { WiaApi.ThrowDeviceError(error); } }