-
Notifications
You must be signed in to change notification settings - Fork 1
/
Percent.cs
145 lines (121 loc) · 5.21 KB
/
Percent.cs
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
using System;
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Windows.Forms;
using DotNetNuke.Services.GeneratedImage;
namespace Bitboxx.Services.GeneratedImage
{
public class Percent:ImageTransform
{
/// <summary>
/// Sets the percentage value for the radial indicator
/// </summary>
public int Percentage { get; set; }
/// <summary>
/// Sets the Color of the indicator element
/// </summary>
public Color Color { get; set; }
/// <summary>
/// Sets the type of the Percentage image (circle,bar)
/// </summary>
public string Type { get; set; }
private enum PercentageType
{
Bar,
Circle
}
public override string UniqueString
{
get { return base.UniqueString + this.Percentage.ToString() + "-" + this.Color.ToString() + "-" +this.Type.ToString(); }
}
public Percent()
{
InterpolationMode = InterpolationMode.HighQualityBicubic;
SmoothingMode = SmoothingMode.HighQuality;
PixelOffsetMode = PixelOffsetMode.HighQuality;
CompositingQuality = CompositingQuality.HighQuality;
}
public override Image ProcessImage(Image image)
{
PercentageType pType;
Bitmap bitmap;
if (!PercentageType.TryParse(Type,true,out pType))
pType = PercentageType.Bar;
if (pType == PercentageType.Bar)
{
bitmap = new Bitmap(500, 50);
using (Graphics objGraphics = Graphics.FromImage(bitmap))
{
// Initialize graphics
objGraphics.Clear(Color.White);
objGraphics.SmoothingMode = SmoothingMode.AntiAlias;
objGraphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
Pen borderPen = new Pen(Color.DarkGray, 6f);
Brush colorBrush = new SolidBrush(Color);
Rectangle barRectangle = new Rectangle(3, 3, Percentage *5 - 6, 44);
objGraphics.FillRectangle(colorBrush, barRectangle);
Rectangle borderRectangle = new Rectangle(3, 3, 494, 44);
objGraphics.DrawRectangle(borderPen, borderRectangle);
// Draw text on image
// Use rectangle for text and align text to center of rectangle
var font = new Font("Arial", 34, FontStyle.Bold);
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
Brush textBrush = new SolidBrush(MostDifferent(Color));
objGraphics.DrawString(Percentage + "%", font, textBrush, borderRectangle, stringFormat);
// Save indicator to file
objGraphics.Flush();
}
}
else
{
bitmap = new Bitmap(1000, 1000);
using (Graphics objGraphics = Graphics.FromImage(bitmap))
{
// Initialize graphics
objGraphics.Clear(Color.White);
objGraphics.SmoothingMode = SmoothingMode.AntiAlias;
objGraphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
Pen borderPen = new Pen(Color.DarkGray, 10f);
// Fill pie
// Degrees are taken clockwise, 0 is parallel with x
// For sweep angle we must convert percent to degrees (90/25 = 18/5)
float startAngle = -90.0F;
float sweepAngle = (18.0F/5)*Percentage;
Rectangle rectangle = new Rectangle(50, 50, 900, 900);
Brush colorBrush = new SolidBrush(Color);
objGraphics.FillPie(colorBrush, rectangle, startAngle, sweepAngle);
// Fill inner circle with white
rectangle = new Rectangle(200, 200, 600, 600);
objGraphics.FillEllipse(Brushes.White, rectangle);
// Draw circles
rectangle = new Rectangle(50, 50, 900, 900);
objGraphics.DrawEllipse(borderPen, rectangle);
rectangle = new Rectangle(200, 200, 600, 600);
objGraphics.DrawEllipse(borderPen, rectangle);
// Draw text on image
// Use rectangle for text and align text to center of rectangle
var font = new Font("Arial", 130, FontStyle.Bold);
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
rectangle = new Rectangle(200, 400, 620, 200);
objGraphics.DrawString(Percentage + "%", font, Brushes.DarkGray, rectangle, stringFormat);
// Save indicator to file
objGraphics.Flush();
}
}
return (Image) bitmap;
}
public Color MostDifferent(Color original)
{
if (original.R + original.G + original.B <= 192 && Percentage > 50)
return System.Drawing.Color.FromArgb(255, 210, 210, 210);
else
return System.Drawing.Color.FromArgb(255, 80, 80, 80);
}
}
}