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