using PaintDotNet.Measurement.HistoryMementos; using System; using System.Drawing; namespace PaintDotNet.Measurement.HistoryFunctions { public sealed class MergeLayerDownFunction : HistoryFunction { public static string StaticName { get { return PdnResources.GetString("MergeLayerDown.HistoryMementoName"); } } public static ImageResource StaticImage { get { return PdnResources.GetImageResource("Icons.MenuLayersMergeLayerDownIcon.png"); } } private int layerIndex; public override HistoryMemento OnExecute(IDocumentWorkspace historyWorkspace) { if (this.layerIndex < 1 || this.layerIndex >= historyWorkspace.GetDocument().Layers.Count) { throw new ArgumentException("layerIndex must be greater than or equal to 1, and a valid layer index. layerIndex=" + layerIndex + ", allowableRange=[0," + historyWorkspace.GetDocument().Layers.Count + ")"); } int bottomLayerIndex = this.layerIndex - 1; Rectangle bounds = historyWorkspace.GetDocument().Bounds; PdnRegion region = new PdnRegion(bounds); BitmapHistoryMemento bhm = new BitmapHistoryMemento( null, null, historyWorkspace, bottomLayerIndex, region); BitmapLayer topLayer = (BitmapLayer)historyWorkspace.GetDocument().Layers[this.layerIndex]; BitmapLayer bottomLayer = (BitmapLayer)historyWorkspace.GetDocument().Layers[bottomLayerIndex]; RenderArgs bottomRA = new RenderArgs(bottomLayer.Surface); EnterCriticalRegion(); topLayer.Render(bottomRA, region); bottomLayer.Invalidate(); bottomRA.Dispose(); bottomRA = null; region.Dispose(); region = null; DeleteLayerFunction dlf = new DeleteLayerFunction(this.layerIndex); HistoryMemento dlhm = dlf.Execute(historyWorkspace); CompoundHistoryMemento chm = new CompoundHistoryMemento(StaticName, StaticImage, new HistoryMemento[] { bhm, dlhm }); return chm; } public MergeLayerDownFunction(int layerIndex) : base(PaintDotNet.Measurement.Enum.ActionFlags.None) { this.layerIndex = layerIndex; } } }