ClipGraphics.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System.Drawing;
  2. using System.Drawing.Drawing2D;
  3. namespace Microsoft.Drawing
  4. {
  5. /// <summary>
  6. /// 暂时修改绘图剪切区,释放时改为原来剪切区
  7. /// </summary>
  8. public sealed class ClipGraphics : DisposableMini
  9. {
  10. private Region m_OldClip; //原始的剪切区
  11. private Graphics m_Graphics; //要修改剪切区的绘图对象
  12. /// <summary>
  13. /// 构造函数
  14. /// </summary>
  15. /// <param name="graphics">绘图对象</param>
  16. /// <param name="g">提供剪切区的绘图对象</param>
  17. /// <param name="combineMode">组合模式</param>
  18. public ClipGraphics(Graphics graphics, Graphics g, CombineMode combineMode)
  19. {
  20. this.m_Graphics = graphics;
  21. this.m_OldClip = graphics.Clip;
  22. graphics.SetClip(g, combineMode);
  23. }
  24. /// <summary>
  25. /// 构造函数
  26. /// </summary>
  27. /// <param name="graphics">绘图对象</param>
  28. /// <param name="path">提供剪切区的路径</param>
  29. /// <param name="combineMode">组合模式</param>
  30. public ClipGraphics(Graphics graphics, GraphicsPath path, CombineMode combineMode)
  31. {
  32. this.m_Graphics = graphics;
  33. this.m_OldClip = graphics.Clip;
  34. graphics.SetClip(path, combineMode);
  35. }
  36. /// <summary>
  37. /// 构造函数
  38. /// </summary>
  39. /// <param name="graphics">绘图对象</param>
  40. /// <param name="rect">提供剪切区的矩形</param>
  41. /// <param name="combineMode">组合模式</param>
  42. public ClipGraphics(Graphics graphics, Rectangle rect, CombineMode combineMode)
  43. {
  44. this.m_Graphics = graphics;
  45. this.m_OldClip = graphics.Clip;
  46. graphics.SetClip(rect, combineMode);
  47. }
  48. /// <summary>
  49. /// 构造函数
  50. /// </summary>
  51. /// <param name="graphics">绘图对象</param>
  52. /// <param name="rect">提供剪切区的矩形</param>
  53. /// <param name="combineMode">组合模式</param>
  54. public ClipGraphics(Graphics graphics, RectangleF rect, CombineMode combineMode)
  55. {
  56. this.m_Graphics = graphics;
  57. this.m_OldClip = graphics.Clip;
  58. graphics.SetClip(rect, combineMode);
  59. }
  60. /// <summary>
  61. /// 构造函数
  62. /// </summary>
  63. /// <param name="graphics">绘图对象</param>
  64. /// <param name="region">提供剪切区的图形</param>
  65. /// <param name="combineMode">组合模式</param>
  66. public ClipGraphics(Graphics graphics, Region region, CombineMode combineMode)
  67. {
  68. this.m_Graphics = graphics;
  69. this.m_OldClip = graphics.Clip;
  70. graphics.SetClip(region, combineMode);
  71. }
  72. /// <summary>
  73. /// 释放资源
  74. /// </summary>
  75. /// <param name="disposing">释放托管资源为true,否则为false</param>
  76. protected override void Dispose(bool disposing)
  77. {
  78. if (this.m_Graphics != null)
  79. {
  80. this.m_Graphics.SetClip(this.m_OldClip, CombineMode.Replace);
  81. this.m_Graphics = null;
  82. }
  83. if (this.m_OldClip != null)
  84. {
  85. this.m_OldClip.Dispose();
  86. this.m_OldClip = null;
  87. }
  88. }
  89. }
  90. }