TranslateGraphics.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System.Drawing;
  2. namespace Microsoft.Drawing
  3. {
  4. /// <summary>
  5. /// 平移变换
  6. /// </summary>
  7. public sealed class TranslateGraphics : DisposableMini
  8. {
  9. private int m_X; //水平平移
  10. private int m_Y; //垂直平移
  11. private Graphics m_Graphics; //要修改剪切区的绘图对象
  12. /// <summary>
  13. /// 构造函数
  14. /// </summary>
  15. /// <param name="graphics">绘图对象</param>
  16. /// <param name="x">水平偏移像素</param>
  17. /// <param name="y">垂直偏移像素</param>
  18. public TranslateGraphics(Graphics graphics, int x, int y)
  19. {
  20. this.m_Graphics = graphics;
  21. this.m_X = x;
  22. this.m_Y = y;
  23. this.m_Graphics.TranslateTransform(this.m_X, this.m_Y);
  24. }
  25. /// <summary>
  26. /// 构造函数
  27. /// </summary>
  28. /// <param name="graphics">绘图对象</param>
  29. /// <param name="p">偏移量</param>
  30. public TranslateGraphics(Graphics graphics, Point p)
  31. {
  32. this.m_Graphics = graphics;
  33. this.m_X = p.X;
  34. this.m_Y = p.Y;
  35. this.m_Graphics.TranslateTransform(this.m_X, this.m_Y);
  36. }
  37. /// <summary>
  38. /// 构造函数
  39. /// </summary>
  40. /// <param name="graphics">绘图对象</param>
  41. /// <param name="s">偏移量</param>
  42. public TranslateGraphics(Graphics graphics, Size s)
  43. {
  44. this.m_Graphics = graphics;
  45. this.m_X = s.Width;
  46. this.m_Y = s.Height;
  47. this.m_Graphics.TranslateTransform(this.m_X, this.m_Y);
  48. }
  49. /// <summary>
  50. /// 释放资源
  51. /// </summary>
  52. /// <param name="disposing">释放托管资源为true,否则为false</param>
  53. protected override void Dispose(bool disposing)
  54. {
  55. if (this.m_Graphics != null)
  56. {
  57. this.m_Graphics.TranslateTransform(-this.m_X, -this.m_Y);
  58. this.m_Graphics = null;
  59. }
  60. this.m_X = 0;
  61. this.m_Y = 0;
  62. }
  63. }
  64. }