public override void PerformAction(AppWorkspace appWorkspace) { if (appWorkspace.CanSetActiveWorkspace) { try { SizeInt32? nullable; IPdnDataObject dataObject; MaskedSurface clipboardImage; try { using (new WaitCursorChanger(appWorkspace)) { CleanupManager.RequestCleanup(); dataObject = PdnClipboard.GetDataObject(); if (ClipboardUtil.IsClipboardImageMaybeAvailable(appWorkspace, dataObject)) { clipboardImage = ClipboardUtil.GetClipboardImage(appWorkspace, dataObject); if (clipboardImage != null) { nullable = new SizeInt32?(clipboardImage.GetCachedGeometryMaskScansBounds().Size); } else { nullable = null; } } else { clipboardImage = null; nullable = null; } } } catch (OutOfMemoryException exception) { ExceptionDialog.ShowErrorDialog(appWorkspace, PdnResources.GetString("PasteAction.Error.OutOfMemory"), exception); return; } catch (Exception exception2) { ExceptionDialog.ShowErrorDialog(appWorkspace, PdnResources.GetString("PasteAction.Error.TransferFromClipboard"), exception2); return; } if (!nullable.HasValue) { MessageBoxUtil.ErrorBox(appWorkspace, PdnResources.GetString("PasteInToNewImageAction.Error.NoClipboardImage")); } else { Type defaultToolType; SizeInt32 size = nullable.Value; Document document = null; if ((appWorkspace.ActiveDocumentWorkspace != null) && (appWorkspace.ActiveDocumentWorkspace.Tool != null)) { defaultToolType = appWorkspace.ActiveDocumentWorkspace.Tool.GetType(); } else { defaultToolType = appWorkspace.DefaultToolType; } using (new WaitCursorChanger(appWorkspace)) { document = new Document(size); DocumentWorkspace documentWorkspace = appWorkspace.AddNewDocumentWorkspace(); documentWorkspace.Document = document; documentWorkspace.History.PushNewMemento(new NullHistoryMemento(string.Empty, null)); PasteInToNewLayerAction action = new PasteInToNewLayerAction(documentWorkspace, dataObject, clipboardImage); if (action.PerformAction()) { using (new PushNullToolMode(documentWorkspace)) { documentWorkspace.Selection.Reset(); documentWorkspace.SetDocumentSaveOptions(null, null, null); documentWorkspace.History.ClearAll(); documentWorkspace.History.PushNewMemento(new NullHistoryMemento(PdnResources.GetString("Menu.Edit.PasteInToNewImage.Text"), PdnResources.GetImageResource("Icons.MenuEditPasteInToNewImageIcon.png"))); appWorkspace.ActiveDocumentWorkspace = documentWorkspace; } documentWorkspace.SetToolFromType(defaultToolType); } else { appWorkspace.RemoveDocumentWorkspace(documentWorkspace); document.Dispose(); } } } } catch (ExternalException exception3) { ExceptionDialog.ShowErrorDialog(appWorkspace, PdnResources.GetString("AcquireImageAction.Error.Clipboard.TransferError"), exception3); } catch (OutOfMemoryException exception4) { ExceptionDialog.ShowErrorDialog(appWorkspace, PdnResources.GetString("AcquireImageAction.Error.Clipboard.OutOfMemory"), exception4); } catch (ThreadStateException) { } } }
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; } } } }
public override void PerformAction(AppWorkspace appWorkspace) { try { IDataObject pasted; Image image; using (new WaitCursorChanger(appWorkspace)) { Utility.GCFullCollect(); pasted = Clipboard.GetDataObject(); image = (Image)pasted.GetData(DataFormats.Bitmap); } if (image == null) { Utility.ErrorBox(appWorkspace, PdnResources.GetString("PasteInToNewImageAction.Error.NoClipboardImage")); } else { Size newSize = image.Size; image.Dispose(); image = null; pasted = null; Document document = null; using (new WaitCursorChanger(appWorkspace)) { document = new Document(newSize); DocumentWorkspace dw = appWorkspace.AddNewDocumentWorkspace(); dw.Document = document; dw.History.PushNewMemento(new NullHistoryMemento(string.Empty, null)); PasteInToNewLayerAction pitnla = new PasteInToNewLayerAction(dw); bool result = pitnla.PerformAction(); if (result) { dw.Selection.Reset(); dw.SetDocumentSaveOptions(null, null, null); dw.History.ClearAll(); dw.History.PushNewMemento( new NullHistoryMemento( PdnResources.GetString("NewImageAction.Name"), PdnResources.GetImageResource("Icons.MenuLayersAddNewLayerIcon.png"))); appWorkspace.ActiveDocumentWorkspace = dw; } else { appWorkspace.RemoveDocumentWorkspace(dw); document.Dispose(); } } } } catch (ExternalException) { Utility.ErrorBox(appWorkspace, PdnResources.GetString("AcquireImageAction.Error.Clipboard.TransferError")); return; } catch (OutOfMemoryException) { Utility.ErrorBox(appWorkspace, PdnResources.GetString("AcquireImageAction.Error.Clipboard.OutOfMemory")); return; } catch (ThreadStateException) { // The ApartmentState property of the application is not set to ApartmentState.STA // I don't think this one will ever happen, seeing as how Main is tagged with the // STA attribute. return; } }
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); } } } } } }