using System.Drawing; using System.Drawing.Drawing2D; namespace Microsoft.Drawing { /// /// 暂时修改绘图剪切区,释放时改为原来剪切区 /// public sealed class ClipGraphics : DisposableMini { private Region m_OldClip; //原始的剪切区 private Graphics m_Graphics; //要修改剪切区的绘图对象 /// /// 构造函数 /// /// 绘图对象 /// 提供剪切区的绘图对象 /// 组合模式 public ClipGraphics(Graphics graphics, Graphics g, CombineMode combineMode) { this.m_Graphics = graphics; this.m_OldClip = graphics.Clip; graphics.SetClip(g, combineMode); } /// /// 构造函数 /// /// 绘图对象 /// 提供剪切区的路径 /// 组合模式 public ClipGraphics(Graphics graphics, GraphicsPath path, CombineMode combineMode) { this.m_Graphics = graphics; this.m_OldClip = graphics.Clip; graphics.SetClip(path, combineMode); } /// /// 构造函数 /// /// 绘图对象 /// 提供剪切区的矩形 /// 组合模式 public ClipGraphics(Graphics graphics, Rectangle rect, CombineMode combineMode) { this.m_Graphics = graphics; this.m_OldClip = graphics.Clip; graphics.SetClip(rect, combineMode); } /// /// 构造函数 /// /// 绘图对象 /// 提供剪切区的矩形 /// 组合模式 public ClipGraphics(Graphics graphics, RectangleF rect, CombineMode combineMode) { this.m_Graphics = graphics; this.m_OldClip = graphics.Clip; graphics.SetClip(rect, combineMode); } /// /// 构造函数 /// /// 绘图对象 /// 提供剪切区的图形 /// 组合模式 public ClipGraphics(Graphics graphics, Region region, CombineMode combineMode) { this.m_Graphics = graphics; this.m_OldClip = graphics.Clip; graphics.SetClip(region, combineMode); } /// /// 释放资源 /// /// 释放托管资源为true,否则为false protected override void Dispose(bool disposing) { if (this.m_Graphics != null) { this.m_Graphics.SetClip(this.m_OldClip, CombineMode.Replace); this.m_Graphics = null; } if (this.m_OldClip != null) { this.m_OldClip.Dispose(); this.m_OldClip = null; } } } }