Example #1
0
        public void Execute(ViewModel viewModel)
        {
            viewModel.Status = string.Empty;
            var request = new ScanRequest()
            {
                DeviceId = viewModel.Scanner.Device.DeviceId,
                Settings = new ScannerSettings
                {
                    PageSize = PageSizes.Letter,
                    ColorDepth = ColorDepths.Color,
                    Orientation = Orientations.Portrait,
                    Resolution = Resolutions.R300,
                    UseAutomaticDocumentFeeder = true
                }
            };

            IScanner proxy = DiscoveryHelper.CreateDiscoveryProxy();
            try
            {
                var response = proxy.Scan(request);

                Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
                dlg.FileName = "Document";
                dlg.DefaultExt = ".pdf";
                dlg.Filter = "Acrobat|*.pdf";

                var result = dlg.ShowDialog();

                if (result == true)
                {
                    using (var file = dlg.OpenFile())
                    {
                        CopyStream(response, file);
                    }
                }
            }
            catch (FaultException<ScanError> e)
            {
                viewModel.Status = e.Detail.ErrorMessage;
            }
            catch (CommunicationException)
            {

            }
            finally
            {
                ICommunicationObject comm = ((ICommunicationObject)proxy);

                if (comm.State == CommunicationState.Faulted)
                {
                    comm.Abort();
                }
                else
                {
                    comm.Close();
                }
            }
        }
Example #2
0
 public System.IO.Stream Scan(ScanRequest request)
 {
     try
     {
         ScanToPDFService svc = new ScanToPDFService(request.DeviceId, request.Settings);
         var pdfDocStream = svc.CreatePDF();
         pdfDocStream.Seek(0, SeekOrigin.Begin);
         return pdfDocStream;
     }
     catch (ScanException se)
     {
         throw ScanError.Fault(se.Message, se);
     }
     catch (Exception e)
     {
         throw ScanError.Fault("Error during scan.", e);
     }
 }