// returns null to indicate user cancelled, or if initialNewSize = newSize that the user requested, // or if there was an error (out of memory) public static Document ResizeDocument(IWin32Window parent, Document document, Size initialNewSize, AnchorEdge initialAnchor, ColorBgra background, bool loadAndSaveMaintainAspect, bool saveAnchor) { using (CanvasSizeDialog csd = new CanvasSizeDialog()) { bool maintainAspect; if (loadAndSaveMaintainAspect) { maintainAspect = Settings.CurrentUser.GetBoolean(SettingNames.LastMaintainAspectRatioCS, false); } else { maintainAspect = false; } csd.OriginalSize = document.Size; csd.OriginalDpuUnit = document.DpuUnit; csd.OriginalDpu = document.DpuX; csd.ImageWidth = initialNewSize.Width; csd.ImageHeight = initialNewSize.Height; csd.LayerCount = document.Layers.Count; csd.AnchorEdge = initialAnchor; csd.Units = csd.OriginalDpuUnit; csd.Resolution = document.DpuX; csd.Units = SettingNames.GetLastNonPixelUnits(); csd.ConstrainToAspect = maintainAspect; DialogResult result = csd.ShowDialog(parent); Size newSize = new Size(csd.ImageWidth, csd.ImageHeight); MeasurementUnit newDpuUnit = csd.Units; double newDpu = csd.Resolution; // If they cancelled, get out if (result == DialogResult.Cancel) { return(null); } // If they clicked OK, then we save the aspect checkbox, and maybe the anchor if (loadAndSaveMaintainAspect) { Settings.CurrentUser.SetBoolean(SettingNames.LastMaintainAspectRatioCS, csd.ConstrainToAspect); } if (saveAnchor) { Settings.CurrentUser.SetString(SettingNames.LastCanvasSizeAnchorEdge, csd.AnchorEdge.ToString()); } if (newSize == document.Size && newDpuUnit == document.DpuUnit && newDpu == document.DpuX) { return(null); } try { Utility.GCFullCollect(); Document newDoc = ResizeDocument(document, newSize, csd.AnchorEdge, background); newDoc.DpuUnit = newDpuUnit; newDoc.DpuX = newDpu; newDoc.DpuY = newDpu; return(newDoc); } catch (OutOfMemoryException) { Utility.ErrorBox(parent, PdnResources.GetString("CanvasSizeAction.ResizeDocument.OutOfMemory")); return(null); } catch { return(null); } } }
public override void PerformAction(AppWorkspace appWorkspace) { using (NewFileDialog nfd = new NewFileDialog()) { Size newDocSize = appWorkspace.GetNewDocumentSize(); if (Utility.IsClipboardImageAvailable()) { try { Utility.GCFullCollect(); IDataObject clipData = System.Windows.Forms.Clipboard.GetDataObject(); using (Image clipImage = (Image)clipData.GetData(DataFormats.Bitmap)) { int width2 = clipImage.Width; int height2 = clipImage.Height; newDocSize = new Size(width2, height2); } } catch (Exception ex) { if (ex is OutOfMemoryException || ex is ExternalException || ex is NullReferenceException) { // ignore } else { throw; } } } nfd.OriginalSize = new Size(newDocSize.Width, newDocSize.Height); nfd.OriginalDpuUnit = SettingNames.GetLastNonPixelUnits(); nfd.OriginalDpu = Document.GetDefaultDpu(nfd.OriginalDpuUnit); nfd.Units = nfd.OriginalDpuUnit; nfd.Resolution = nfd.OriginalDpu; nfd.ConstrainToAspect = Settings.CurrentUser.GetBoolean(SettingNames.LastMaintainAspectRatioNF, false); DialogResult dr = nfd.ShowDialog(appWorkspace); if (dr == DialogResult.OK) { bool success = appWorkspace.CreateBlankDocumentInNewWorkspace(new Size(nfd.ImageWidth, nfd.ImageHeight), nfd.Units, nfd.Resolution, false); if (success) { appWorkspace.ActiveDocumentWorkspace.ZoomBasis = ZoomBasis.FitToWindow; Settings.CurrentUser.SetBoolean(SettingNames.LastMaintainAspectRatioNF, nfd.ConstrainToAspect); if (nfd.Units != MeasurementUnit.Pixel) { Settings.CurrentUser.SetString(SettingNames.LastNonPixelUnits, nfd.Units.ToString()); } if (appWorkspace.Units != MeasurementUnit.Pixel) { appWorkspace.Units = nfd.Units; } } } } }
public override HistoryMemento PerformAction(DocumentWorkspace documentWorkspace) { int newWidth; int newHeight; double newDpu; MeasurementUnit newDpuUnit; string resamplingAlgorithm = Settings.CurrentUser.GetString(SettingNames.LastResamplingMethod, ResamplingAlgorithm.SuperSampling.ToString()); ResamplingAlgorithm alg; try { alg = (ResamplingAlgorithm)Enum.Parse(typeof(ResamplingAlgorithm), resamplingAlgorithm, true); } catch { alg = ResamplingAlgorithm.SuperSampling; } bool maintainAspect = Settings.CurrentUser.GetBoolean(SettingNames.LastMaintainAspectRatio, true); using (ResizeDialog rd = new ResizeDialog()) { rd.OriginalSize = documentWorkspace.Document.Size; rd.OriginalDpuUnit = documentWorkspace.Document.DpuUnit; rd.OriginalDpu = documentWorkspace.Document.DpuX; rd.ImageHeight = documentWorkspace.Document.Height; rd.ImageWidth = documentWorkspace.Document.Width; rd.ResamplingAlgorithm = alg; rd.LayerCount = documentWorkspace.Document.Layers.Count; rd.Units = rd.OriginalDpuUnit; rd.Resolution = documentWorkspace.Document.DpuX; rd.Units = SettingNames.GetLastNonPixelUnits(); rd.ConstrainToAspect = maintainAspect; DialogResult result = rd.ShowDialog(documentWorkspace); if (result == DialogResult.Cancel) { return(null); } Settings.CurrentUser.SetString(SettingNames.LastResamplingMethod, rd.ResamplingAlgorithm.ToString()); Settings.CurrentUser.SetBoolean(SettingNames.LastMaintainAspectRatio, rd.ConstrainToAspect); newDpuUnit = rd.Units; newWidth = rd.ImageWidth; newHeight = rd.ImageHeight; newDpu = rd.Resolution; alg = rd.ResamplingAlgorithm; if (newDpuUnit != MeasurementUnit.Pixel) { Settings.CurrentUser.SetString(SettingNames.LastNonPixelUnits, newDpuUnit.ToString()); if (documentWorkspace.AppWorkspace.Units != MeasurementUnit.Pixel) { documentWorkspace.AppWorkspace.Units = newDpuUnit; } } // if the new size equals the old size, there's really no point in doing anything if (documentWorkspace.Document.Size == new Size(rd.ImageWidth, rd.ImageHeight) && documentWorkspace.Document.DpuX == newDpu && documentWorkspace.Document.DpuUnit == newDpuUnit) { return(null); } } HistoryMemento ha; if (newWidth == documentWorkspace.Document.Width && newHeight == documentWorkspace.Document.Height) { // Only adjusting Dpu or DpuUnit ha = new MetaDataHistoryMemento(StaticName, StaticImage, documentWorkspace); documentWorkspace.Document.DpuUnit = newDpuUnit; documentWorkspace.Document.DpuX = newDpu; documentWorkspace.Document.DpuY = newDpu; } else { try { using (new WaitCursorChanger(documentWorkspace)) { ha = new ReplaceDocumentHistoryMemento(StaticName, StaticImage, documentWorkspace); } Document newDocument = new Document(newWidth, newHeight); newDocument.ReplaceMetaDataFrom(documentWorkspace.Document); newDocument.DpuUnit = newDpuUnit; newDocument.DpuX = newDpu; newDocument.DpuY = newDpu; ResizeProgressDialog rpd = new ResizeProgressDialog(documentWorkspace, newDocument, documentWorkspace.Document, new Size(newWidth, newHeight), alg); Utility.GCFullCollect(); bool result = rpd.DoResize(); if (!result) { return(null); } documentWorkspace.Document = newDocument; } catch (WorkerThreadException ex) { if (ex.InnerException is OutOfMemoryException) { Utility.ErrorBox(documentWorkspace, PdnResources.GetString("ResizeAction.PerformAction.OutOfMemory")); return(null); } else { throw; } } catch (OutOfMemoryException) { Utility.ErrorBox(documentWorkspace, PdnResources.GetString("ResizeAction.PerformAction.OutOfMemory")); return(null); } } return(ha); }