public static Document ResizeDocument(Document document, Size newSize, AnchorEdge edge, ColorBgra background) { Document newDoc = new Document(newSize.Width, newSize.Height); newDoc.ReplaceMetaDataFrom(document); for (int i = 0; i < document.Layers.Count; ++i) { Layer layer = (Layer)document.Layers[i]; if (layer is BitmapLayer) { Layer newLayer; try { newLayer = ResizeLayer((BitmapLayer)layer, newSize, edge, background); } catch (OutOfMemoryException) { newDoc.Dispose(); throw; } newDoc.Layers.Add(newLayer); } else { throw new InvalidOperationException("Canvas Size does not support Layers that are not BitmapLayers"); } } return newDoc; }
public override HistoryMemento PerformAction(DocumentWorkspace documentWorkspace) { int newWidth; int newHeight; double newDpu; MeasurementUnit newDpuUnit; string resamplingAlgorithm = Settings.CurrentUser.GetString(PdnSettings.LastResamplingMethod, ResamplingAlgorithm.SuperSampling.ToString()); ResamplingAlgorithm alg; try { alg = (ResamplingAlgorithm)Enum.Parse(typeof(ResamplingAlgorithm), resamplingAlgorithm, true); } catch { alg = ResamplingAlgorithm.SuperSampling; } bool maintainAspect = Settings.CurrentUser.GetBoolean(PdnSettings.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 = PdnSettings.GetLastNonPixelUnits(); rd.ConstrainToAspect = maintainAspect; DialogResult result = rd.ShowDialog(documentWorkspace); if (result == DialogResult.Cancel) { return null; } Settings.CurrentUser.SetString(PdnSettings.LastResamplingMethod, rd.ResamplingAlgorithm.ToString()); Settings.CurrentUser.SetBoolean(PdnSettings.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(PdnSettings.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; }