MergeLayerDownFunction.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using PaintDotNet.Measurement.HistoryMementos;
  2. using System;
  3. using System.Drawing;
  4. namespace PaintDotNet.Measurement.HistoryFunctions
  5. {
  6. public sealed class MergeLayerDownFunction
  7. : HistoryFunction
  8. {
  9. public static string StaticName
  10. {
  11. get
  12. {
  13. return PdnResources.GetString("MergeLayerDown.HistoryMementoName");
  14. }
  15. }
  16. public static ImageResource StaticImage
  17. {
  18. get
  19. {
  20. return PdnResources.GetImageResource("Icons.MenuLayersMergeLayerDownIcon.png");
  21. }
  22. }
  23. private int layerIndex;
  24. public override HistoryMemento OnExecute(IDocumentWorkspace historyWorkspace)
  25. {
  26. if (this.layerIndex < 1 || this.layerIndex >= historyWorkspace.GetDocument().Layers.Count)
  27. {
  28. throw new ArgumentException("layerIndex must be greater than or equal to 1, and a valid layer index. layerIndex=" +
  29. layerIndex + ", allowableRange=[0," + historyWorkspace.GetDocument().Layers.Count + ")");
  30. }
  31. int bottomLayerIndex = this.layerIndex - 1;
  32. Rectangle bounds = historyWorkspace.GetDocument().Bounds;
  33. PdnRegion region = new PdnRegion(bounds);
  34. BitmapHistoryMemento bhm = new BitmapHistoryMemento(
  35. null,
  36. null,
  37. historyWorkspace,
  38. bottomLayerIndex,
  39. region);
  40. BitmapLayer topLayer = (BitmapLayer)historyWorkspace.GetDocument().Layers[this.layerIndex];
  41. BitmapLayer bottomLayer = (BitmapLayer)historyWorkspace.GetDocument().Layers[bottomLayerIndex];
  42. RenderArgs bottomRA = new RenderArgs(bottomLayer.Surface);
  43. EnterCriticalRegion();
  44. topLayer.Render(bottomRA, region);
  45. bottomLayer.Invalidate();
  46. bottomRA.Dispose();
  47. bottomRA = null;
  48. region.Dispose();
  49. region = null;
  50. DeleteLayerFunction dlf = new DeleteLayerFunction(this.layerIndex);
  51. HistoryMemento dlhm = dlf.Execute(historyWorkspace);
  52. CompoundHistoryMemento chm = new CompoundHistoryMemento(StaticName, StaticImage, new HistoryMemento[] { bhm, dlhm });
  53. return chm;
  54. }
  55. public MergeLayerDownFunction(int layerIndex)
  56. : base(PaintDotNet.Measurement.Enum.ActionFlags.None)
  57. {
  58. this.layerIndex = layerIndex;
  59. }
  60. }
  61. }