-
Notifications
You must be signed in to change notification settings - Fork 1
/
Thermometer.cs
91 lines (73 loc) · 3.43 KB
/
Thermometer.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
using System;
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using DotNetNuke.Services.GeneratedImage;
namespace Bitboxx.Services.GeneratedImage
{
public class Thermometer : ImageTransform
{
public string Degree { get; set; }
public string Areas { get; set; }
public override string UniqueString
{
get { return base.UniqueString + this.Degree + this.Areas; }
}
public Thermometer()
{
InterpolationMode = InterpolationMode.HighQualityBicubic;
SmoothingMode = SmoothingMode.HighQuality;
PixelOffsetMode = PixelOffsetMode.HighQuality;
CompositingQuality = CompositingQuality.HighQuality;
}
public override Image ProcessImage(Image image)
{
// create new "canvas" for the new image in this case
// exactly the size of the thermometer image in folder images
Bitmap bitmap = new Bitmap(126,574);
if (String.IsNullOrEmpty(Areas) || Areas.Length - Areas.Replace(",", "").Length != 3)
Areas = "20,30,40,60";
string[] areas = Areas.Split(',');
int[] iAreas = new int[] {Convert.ToInt16(areas[0]), Convert.ToInt16(areas[1]), Convert.ToInt16(areas[2]), Convert.ToInt16(areas[3])};
using (Graphics objGraphics = Graphics.FromImage(bitmap))
{
// Initialize graphics
objGraphics.Clear(Color.White);
// Convert Parameters from string to whats needed.
decimal degree = Convert.ToDecimal(Degree);
// Retrieve color for circle and bar
Brush colorBrush = GetThermometerColor(degree,iAreas);
// Draw the circle on a fixed position
objGraphics.FillEllipse(colorBrush, new Rectangle(1, 485, 82, 82));
// Do some math to determine the height of the thermometerbar and draw it
int height = Convert.ToInt32((decimal) 450 / 100 * degree + 15);
objGraphics.FillRectangle(colorBrush, 24, 490 - height, 40, height);
// Load the image - which is included in the project with property:
// Build Action = Embedded Resouce
System.Reflection.Assembly thisExe = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream file = thisExe.GetManifestResourceStream("Bitboxx.Services.GeneratedImage.Images.Thermometer.png");
Bitmap bmp = (Bitmap)Image.FromStream(file);
//.. and draw it to the canvas as a layer on top
objGraphics.DrawImageUnscaled(bmp, new Point(0, 0));
// we are done and save it and return the image
objGraphics.Flush();
}
return (Image) bitmap;
}
private static Brush GetThermometerColor(decimal degree, int[] areas)
{
Color barColor = Color.RoyalBlue;
if (degree >= areas[3])
barColor = Color.Purple;
else if (degree >= areas[2])
barColor = Color.Red;
else if (degree >= areas[1])
barColor = Color.Orange;
else if (degree >= areas[0])
barColor = Color.LimeGreen;
Brush barBrush = new SolidBrush(barColor);
return barBrush;
}
}
}