Exemplo n.º 1
0
        public override HistoryMemento PerformAction(DocumentWorkspace documentWorkspace)
        {
            if (!ScanningAndPrinting.CanPrint)
            {
                Utility.ShowWiaError(documentWorkspace);
                return(null);
            }

            using (new PushNullToolMode(documentWorkspace))
            {
                // render image to a bitmap, save it to disk
                Surface scratch = documentWorkspace.BorrowScratchSurface(this.GetType().Name + ".PerformAction()");

                try
                {
                    scratch.Clear();
                    RenderArgs ra = new RenderArgs(scratch);

                    documentWorkspace.Update();

                    using (new WaitCursorChanger(documentWorkspace))
                    {
                        ra.Surface.Clear(ColorBgra.White);
                        documentWorkspace.Document.Render(ra, false);
                    }

                    string tempName = Path.GetTempFileName() + ".bmp";
                    ra.Bitmap.Save(tempName, ImageFormat.Bmp);

                    try
                    {
                        ScanningAndPrinting.Print(documentWorkspace, tempName);
                    }

                    catch (Exception ex)
                    {
                        Utility.ShowWiaError(documentWorkspace);
                        Tracing.Ping(ex.ToString());
                        // TODO: do a "better" error dialog here
                    }

                    // Try to delete the temp file but don't worry if we can't
                    bool result = FileSystem.TryDeleteFile(tempName);
                }

                finally
                {
                    documentWorkspace.ReturnScratchSurface(scratch);
                }
            }

            return(null);
        }
Exemplo n.º 2
0
 public override HistoryMemento PerformAction(DocumentWorkspace documentWorkspace)
 {
     if (!ScanningAndPrinting.CanPrint)
     {
         ShowWiaError(documentWorkspace);
         return(null);
     }
     using (new PushNullToolMode(documentWorkspace))
     {
         Surface surface = documentWorkspace.BorrowScratchSurface(base.GetType().Name + ".PerformAction()");
         try
         {
             surface.Clear();
             RenderArgs args = new RenderArgs(surface);
             documentWorkspace.QueueUpdate();
             using (new WaitCursorChanger(documentWorkspace))
             {
                 args.Surface.Clear(ColorBgra.White);
                 documentWorkspace.Document.Render(args, false);
             }
             string filename = Path.GetTempFileName() + ".bmp";
             args.Bitmap.Save(filename, ImageFormat.Bmp);
             try
             {
                 ScanningAndPrinting.Print(documentWorkspace, filename);
             }
             catch (Exception)
             {
                 ShowWiaError(documentWorkspace);
             }
             bool flag = FileSystem.TryDeleteFile(filename);
         }
         catch (Exception exception)
         {
             ExceptionDialog.ShowErrorDialog(documentWorkspace, exception);
         }
         finally
         {
             documentWorkspace.ReturnScratchSurface(surface);
         }
     }
     return(null);
 }
        public override void PerformAction(AppWorkspace appWorkspace)
        {
            if (!ScanningAndPrinting.CanScan)
            {
                Utility.ShowWiaError(appWorkspace);
                return;
            }

            string     tempName = Path.ChangeExtension(SystemLayer.FileSystem.GetTempFileName(), ".bmp");
            ScanResult result;

            try
            {
                result = ScanningAndPrinting.Scan(appWorkspace, tempName);
            }

            // If there was an exception, let's assume the user has already received an error dialog,
            // either from Windows or from the WIA UI, and let's /not/ present another error dialog.
            catch (Exception)
            {
                result = ScanResult.UserCancelled;
            }

            if (result == ScanResult.Success)
            {
                string errorText = null;

                try
                {
                    Image image;

                    try
                    {
                        image = PdnResources.LoadImage(tempName);
                    }

                    catch (FileNotFoundException)
                    {
                        errorText = PdnResources.GetString("LoadImage.Error.FileNotFoundException");
                        throw;
                    }

                    catch (OutOfMemoryException)
                    {
                        errorText = PdnResources.GetString("LoadImage.Error.OutOfMemoryException");
                        throw;
                    }

                    Document document;

                    try
                    {
                        document = Document.FromImage(image);
                    }

                    catch (OutOfMemoryException)
                    {
                        errorText = PdnResources.GetString("LoadImage.Error.OutOfMemoryException");
                        throw;
                    }

                    finally
                    {
                        image.Dispose();
                        image = null;
                    }

                    DocumentWorkspace dw = appWorkspace.AddNewDocumentWorkspace();

                    try
                    {
                        dw.Document = document;
                    }

                    catch (OutOfMemoryException)
                    {
                        errorText = PdnResources.GetString("LoadImage.Error.OutOfMemoryException");
                        throw;
                    }

                    document = null;
                    dw.SetDocumentSaveOptions(null, null, null);
                    dw.History.ClearAll();

                    HistoryMemento newHA = new NullHistoryMemento(
                        PdnResources.GetString("AcquireImageAction.Name"),
                        ImageResource.Get("Icons.MenuLayersAddNewLayerIcon.png"));

                    dw.History.PushNewMemento(newHA);

                    appWorkspace.ActiveDocumentWorkspace = dw;

                    // Try to delete the temp file but don't worry if we can't
                    try
                    {
                        File.Delete(tempName);
                    }

                    catch
                    {
                    }
                }

                catch (Exception)
                {
                    if (errorText != null)
                    {
                        Utility.ErrorBox(appWorkspace, errorText);
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
Exemplo n.º 4
0
 public override void PerformAction(AppWorkspace appWorkspace)
 {
     if (appWorkspace.CanSetActiveWorkspace)
     {
         if (!ScanningAndPrinting.CanScan)
         {
             ShowWiaError(appWorkspace);
         }
         else
         {
             ScanResult userCancelled;
             string     fileName = Path.ChangeExtension(FileSystem.GetTempFileName(), ".bmp");
             try
             {
                 userCancelled = ScanningAndPrinting.Scan(appWorkspace, fileName);
             }
             catch (Exception)
             {
                 userCancelled = ScanResult.UserCancelled;
             }
             if (userCancelled == ScanResult.Success)
             {
                 string str2 = null;
                 try
                 {
                     Image    image;
                     Document document;
                     try
                     {
                         using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
                         {
                             image = Image.FromStream(stream, false, true);
                         }
                     }
                     catch (FileNotFoundException)
                     {
                         str2 = PdnResources.GetString("LoadImage.Error.FileNotFoundException");
                         throw;
                     }
                     catch (OutOfMemoryException)
                     {
                         str2 = PdnResources.GetString("LoadImage.Error.OutOfMemoryException");
                         throw;
                     }
                     catch (Exception)
                     {
                         str2 = string.Empty;
                         throw;
                     }
                     try
                     {
                         document = Document.FromGdipImage(image, false);
                     }
                     catch (OutOfMemoryException)
                     {
                         str2 = PdnResources.GetString("LoadImage.Error.OutOfMemoryException");
                         throw;
                     }
                     catch (Exception)
                     {
                         str2 = string.Empty;
                         throw;
                     }
                     finally
                     {
                         image.Dispose();
                         image = null;
                     }
                     DocumentWorkspace workspace = appWorkspace.AddNewDocumentWorkspace();
                     try
                     {
                         workspace.Document = document;
                     }
                     catch (OutOfMemoryException)
                     {
                         str2 = PdnResources.GetString("LoadImage.Error.OutOfMemoryException");
                         throw;
                     }
                     document = null;
                     workspace.SetDocumentSaveOptions(null, null, null);
                     workspace.History.ClearAll();
                     HistoryMemento memento = new NullHistoryMemento(PdnResources.GetString("AcquireImageAction.Name"), PdnResources.GetImageResource("Icons.MenuLayersAddNewLayerIcon.png"));
                     workspace.History.PushNewMemento(memento);
                     appWorkspace.ActiveDocumentWorkspace = workspace;
                     try
                     {
                         File.Delete(fileName);
                     }
                     catch (Exception)
                     {
                     }
                 }
                 catch (Exception exception)
                 {
                     if (str2 == null)
                     {
                         throw;
                     }
                     if (string.IsNullOrEmpty(str2))
                     {
                         ExceptionDialog.ShowErrorDialog(appWorkspace, exception);
                     }
                     else
                     {
                         ExceptionDialog.ShowErrorDialog(appWorkspace, str2, exception);
                     }
                 }
             }
         }
     }
 }