BufferedGraphicsEx.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using System.Drawing;
  3. using System.Reflection;
  4. namespace Microsoft.Drawing
  5. {
  6. /// <summary>
  7. /// BufferedGraphicsEx辅助类
  8. /// </summary>
  9. public static class BufferedGraphicsEx
  10. {
  11. private static readonly FieldInfo FiTargetLoc; //BufferedGraphics.targetLoc字段信息
  12. private static readonly FieldInfo FiVirtulSize; //BufferedGraphics.virtualSize字段信息
  13. /// <summary>
  14. /// 静态构造
  15. /// </summary>
  16. static BufferedGraphicsEx()
  17. {
  18. Type type = typeof(BufferedGraphics);
  19. BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
  20. FiTargetLoc = type.GetField("targetLoc", flags);
  21. FiVirtulSize = type.GetField("virtualSize", flags);
  22. }
  23. /// <summary>
  24. /// 获取缓冲区的目标坐标。
  25. /// </summary>
  26. /// <param name="bg">缓冲区。</param>
  27. /// <returns>缓冲区的目标坐标</returns>
  28. public static Point GetTargetLoc(BufferedGraphics bg)
  29. {
  30. if (bg == null)
  31. throw new ArgumentNullException("bg");
  32. return (Point)FiTargetLoc.GetValue(bg);
  33. }
  34. /// <summary>
  35. /// 获取缓冲区的虚拟尺寸大小。
  36. /// </summary>
  37. /// <param name="bg">缓冲区。</param>
  38. /// <returns>缓冲区的虚拟尺寸大小。</returns>
  39. public static Size GetVirtualSize(BufferedGraphics bg)
  40. {
  41. if (bg == null)
  42. throw new ArgumentNullException("bg");
  43. return (Size)FiVirtulSize.GetValue(bg);
  44. }
  45. /// <summary>
  46. /// 将图形缓冲区的内容写入指定的 System.Drawing.Graphics 对象。
  47. /// </summary>
  48. /// <param name="bgSrc">图形缓冲区,要混合的源。</param>
  49. /// <param name="gDest">一个 System.Drawing.Graphics 对象,要向其中写入图形缓冲区的内容。</param>
  50. /// <param name="rcDest">目标矩形。</param>
  51. public static void Render(BufferedGraphics bgSrc, Graphics gDest, Rectangle rcDest)
  52. {
  53. //验证
  54. if (bgSrc == null || gDest == null || !RectangleEx.IsVisible(rcDest))
  55. return;
  56. //反射获取私有字段
  57. Point targetLoc = (Point)FiTargetLoc.GetValue(bgSrc);
  58. targetLoc.Offset(rcDest.X, rcDest.Y);
  59. Size virtualSize = rcDest.Size;
  60. Point sourceLoc = rcDest.Location;
  61. //混合渲染
  62. GraphicsEx.Render(bgSrc.Graphics, gDest, targetLoc, sourceLoc, virtualSize);
  63. }
  64. /// <summary>
  65. /// 将图形缓冲区的内容与指定的 System.Drawing.Graphics 对象混合。(支持缓冲区Alpha通道)
  66. /// </summary>
  67. /// <param name="bgSrc"></param>
  68. /// <param name="gDest">一个 System.Drawing.Graphics 对象,图形缓冲区要混合的目标。</param>
  69. public static void BlendRender(BufferedGraphics bgSrc, Graphics gDest)
  70. {
  71. //验证
  72. if (bgSrc == null || gDest == null)
  73. return;
  74. //反射获取私有字段
  75. Point targetLoc = (Point)FiTargetLoc.GetValue(bgSrc);
  76. Size virtualSize = (Size)FiVirtulSize.GetValue(bgSrc);
  77. //混合渲染
  78. GraphicsEx.BlendRender(bgSrc.Graphics, gDest, targetLoc, Point.Empty, virtualSize);
  79. }
  80. /// <summary>
  81. /// 将图形缓冲区的内容与指定的 System.Drawing.Graphics 对象混合。(支持缓冲区Alpha通道)
  82. /// </summary>
  83. /// <param name="bgSrc">图形缓冲区,要混合的源。</param>
  84. /// <param name="gDest">一个 System.Drawing.Graphics 对象,图形缓冲区要混合的目标。</param>
  85. /// <param name="rcDest">目标矩形。</param>
  86. public static void BlendRender(BufferedGraphics bgSrc, Graphics gDest, Rectangle rcDest)
  87. {
  88. //验证
  89. if (bgSrc == null || gDest == null || !RectangleEx.IsVisible(rcDest))
  90. return;
  91. //反射获取私有字段
  92. Point targetLoc = (Point)FiTargetLoc.GetValue(bgSrc);
  93. targetLoc.Offset(rcDest.X, rcDest.Y);
  94. Size virtualSize = rcDest.Size;
  95. Point sourceLoc = rcDest.Location;
  96. //混合渲染
  97. GraphicsEx.BlendRender(bgSrc.Graphics, gDest, targetLoc, sourceLoc, virtualSize);
  98. }
  99. }
  100. }