tangxu
2025-02-10 e40718947b5aa9205c87a7478aa86c37a82e0510
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#region Imports
 
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Extension.Metro
{
    #region MethodsExtension
 
    internal class Methods
    {
        public static void DrawImageFromBase64(Graphics graphics, string base64Image, Rectangle rect)
        {
            Image im;
            using (System.IO.MemoryStream ms = new(Convert.FromBase64String(base64Image)))
            {
                im = Image.FromStream(ms);
                ms.Close();
            }
            graphics.DrawImage(im, rect);
        }
 
        public static void DrawImageWithColor(Graphics G, Rectangle r, Image image, Color c)
        {
            float[][] ptsArray = new[]
            {
                new[] {Convert.ToSingle(c.R / 255.0), 0f, 0f, 0f, 0f},
                new[] {0f, Convert.ToSingle(c.G / 255.0), 0f, 0f, 0f},
                new[] {0f, 0f, Convert.ToSingle(c.B / 255.0), 0f, 0f},
                new[] {0f, 0f, 0f, Convert.ToSingle(c.A / 255.0), 0f},
                new[]
                {
                    Convert.ToSingle( c.R/255.0),
                    Convert.ToSingle( c.G/255.0),
                    Convert.ToSingle( c.B/255.0), 0f,
                    Convert.ToSingle( c.A/255.0)
                }
            };
            ImageAttributes imageAttributes = new();
            imageAttributes.SetColorMatrix(new ColorMatrix(ptsArray), ColorMatrixFlag.Default, ColorAdjustType.Default);
            G.DrawImage(image, r, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttributes);
            image.Dispose();
        }
 
        public static void DrawImageWithColor(Graphics G, Rectangle r, string image, Color c)
        {
            Image im = ImageFromBase64(image);
            float[][] ptsArray = new[]
            {
                new[] {Convert.ToSingle(c.R / 255.0), 0f, 0f, 0f, 0f},
                new[] {0f, Convert.ToSingle(c.G / 255.0), 0f, 0f, 0f},
                new[] {0f, 0f, Convert.ToSingle(c.B / 255.0), 0f, 0f},
                new[] {0f, 0f, 0f, Convert.ToSingle(c.A / 255.0), 0f},
                new[]
                {
                    Convert.ToSingle( c.R/255.0),
                    Convert.ToSingle( c.G/255.0),
                    Convert.ToSingle( c.B/255.0), 0f,
                    Convert.ToSingle( c.A/255.0)
                }
            };
            ImageAttributes imageAttributes = new();
            imageAttributes.SetColorMatrix(new ColorMatrix(ptsArray), ColorMatrixFlag.Default, ColorAdjustType.Default);
            G.DrawImage(im, r, 0, 0, im.Width, im.Height, GraphicsUnit.Pixel, imageAttributes);
        }
 
        public StringFormat SetPosition(StringAlignment horizontal = StringAlignment.Center, StringAlignment vertical = StringAlignment.Center)
        {
            return new StringFormat
            {
                Alignment = horizontal,
                LineAlignment = vertical
            };
        }
 
        public static float[][] ColorToMatrix(float alpha, Color c)
        {
            return new[]
            {
                new [] {Convert.ToSingle(c.R / 255),0,0,0,0},
                new [] {0,Convert.ToSingle(c.G / 255),0,0,0},
                new [] {0,0,Convert.ToSingle(c.B / 255),0,0},
                new [] {0,0,0,Convert.ToSingle(c.A / 255),0},
                new []
                {
                    Convert.ToSingle(c.R / 255),
                    Convert.ToSingle(c.G / 255),
                    Convert.ToSingle(c.B / 255),
                    alpha,
                    Convert.ToSingle(c.A / 255)
                }
            };
        }
 
        public void DrawImageWithTransparency(Graphics G, float alpha, Image image, Rectangle rect)
        {
            ColorMatrix colorMatrix = new() { Matrix33 = alpha };
            ImageAttributes imageAttributes = new();
            imageAttributes.SetColorMatrix(colorMatrix);
            G.DrawImage(image, new Rectangle(rect.X, rect.Y, image.Width, image.Height), rect.X, rect.Y, image.Width, image.Height, GraphicsUnit.Pixel, imageAttributes);
            imageAttributes.Dispose();
        }
 
        public static Image ImageFromBase64(string base64Image)
        {
            using System.IO.MemoryStream ms = new(Convert.FromBase64String(base64Image));
            return Image.FromStream(ms);
        }
 
        public static GraphicsPath RoundRec(Rectangle r, int curve, bool topLeft = true, bool topRight = true, bool bottomLeft = true, bool bottomRight = true)
        {
            GraphicsPath createRoundPath = new(FillMode.Winding);
            if (topLeft)
            {
                createRoundPath.AddArc(r.X, r.Y, curve, curve, 180f, 90f);
            }
            else
            {
                createRoundPath.AddLine(r.X, r.Y, r.X, r.Y);
            }
 
            if (topRight)
            {
                createRoundPath.AddArc(r.Right - curve, r.Y, curve, curve, 270f, 90f);
            }
            else
            {
                createRoundPath.AddLine(r.Right - r.Width, r.Y, r.Width, r.Y);
            }
 
            if (bottomRight)
            {
                createRoundPath.AddArc(r.Right - curve, r.Bottom - curve, curve, curve, 0f, 90f);
            }
            else
            {
                createRoundPath.AddLine(r.Right, r.Bottom, r.Right, r.Bottom);
            }
 
            if (bottomLeft)
            {
                createRoundPath.AddArc(r.X, r.Bottom - curve, curve, curve, 90f, 90f);
            }
            else
            {
                createRoundPath.AddLine(r.X, r.Bottom, r.X, r.Bottom);
            }
 
            createRoundPath.CloseFigure();
            return createRoundPath;
        }
 
        public static GraphicsPath RoundRec(int x, int y, int width, int height, int curve, bool topLeft = true, bool topRight = true, bool bottomLeft = true, bool bottomRight = true)
        {
            Rectangle r = new(x, y, width, height);
            GraphicsPath createRoundPath = new(FillMode.Winding);
            if (topLeft)
            {
                createRoundPath.AddArc(r.X, r.Y, curve, curve, 180f, 90f);
            }
            else
            {
                createRoundPath.AddLine(r.X, r.Y, r.X, r.Y);
            }
 
            if (topRight)
            {
                createRoundPath.AddArc(r.Right - curve, r.Y, curve, curve, 270f, 90f);
            }
            else
            {
                createRoundPath.AddLine(r.Right - r.Width, r.Y, r.Width, r.Y);
            }
 
            if (bottomRight)
            {
                createRoundPath.AddArc(r.Right - curve, r.Bottom - curve, curve, curve, 0f, 90f);
            }
            else
            {
                createRoundPath.AddLine(r.Right, r.Bottom, r.Right, r.Bottom);
            }
 
            if (bottomLeft)
            {
                createRoundPath.AddArc(r.X, r.Bottom - curve, curve, curve, 90f, 90f);
            }
            else
            {
                createRoundPath.AddLine(r.X, r.Bottom, r.X, r.Bottom);
            }
 
            createRoundPath.CloseFigure();
            return createRoundPath;
        }
    }
 
    #endregion
}