TextRenderingHintGraphics.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System.Drawing;
  2. using System.Drawing.Text;
  3. namespace Microsoft.Drawing
  4. {
  5. /// <summary>
  6. /// 暂时修改绘图对象的文本呈现模式,释放时改为原来模式
  7. /// </summary>
  8. public sealed class TextRenderingHintGraphics : DisposableMini
  9. {
  10. private TextRenderingHint m_OldHint; //原始的文本呈现模式
  11. private Graphics m_Graphics; //要修改文本呈现模式的绘图对象
  12. /// <summary>
  13. /// 构造函数,暂时修改为抗锯齿
  14. /// </summary>
  15. /// <param name="graphics">绘图对象</param>
  16. public TextRenderingHintGraphics(Graphics graphics)
  17. : this(graphics, TextRenderingHint.AntiAlias)
  18. {
  19. }
  20. /// <summary>
  21. /// 构造函数
  22. /// </summary>
  23. /// <param name="graphics">绘图对象</param>
  24. /// <param name="newHint">新文本呈现模式</param>
  25. public TextRenderingHintGraphics(Graphics graphics, TextRenderingHint newHint)
  26. {
  27. this.m_Graphics = graphics;
  28. this.m_OldHint = graphics.TextRenderingHint;
  29. graphics.TextRenderingHint = newHint;
  30. }
  31. /// <summary>
  32. /// 释放资源
  33. /// </summary>
  34. /// <param name="disposing">释放托管资源为true,否则为false</param>
  35. protected override void Dispose(bool disposing)
  36. {
  37. if (this.m_Graphics != null)
  38. {
  39. this.m_Graphics.TextRenderingHint = this.m_OldHint;
  40. this.m_Graphics = null;
  41. }
  42. this.m_OldHint = TextRenderingHint.SystemDefault;
  43. }
  44. }
  45. }