forked from migueldeicaza/TensorFlowSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageEditor.cs
More file actions
78 lines (65 loc) · 2.08 KB
/
ImageEditor.cs
File metadata and controls
78 lines (65 loc) · 2.08 KB
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
using System;
using System.Drawing;
namespace ExampleCommon
{
/// <summary>
/// Allows to add graphic elements to the existing image.
/// </summary>
public class ImageEditor : IDisposable
{
private Graphics _graphics;
private Image _image;
private string _fontFamily;
private float _fontSize;
private string _outputFile;
public ImageEditor (string inputFile, string outputFile, string fontFamily = "Ariel", float fontSize = 12)
{
if (string.IsNullOrEmpty (inputFile)) {
throw new ArgumentNullException (nameof (inputFile));
}
if (string.IsNullOrEmpty (outputFile)) {
throw new ArgumentNullException (nameof (outputFile));
}
_fontFamily = fontFamily;
_fontSize = fontSize;
_outputFile = outputFile;
_image = Bitmap.FromFile (inputFile);
_graphics = Graphics.FromImage (_image);
}
/// <summary>
/// Adds rectangle with a label in particular position of the image
/// </summary>
/// <param name="xmin"></param>
/// <param name="xmax"></param>
/// <param name="ymin"></param>
/// <param name="ymax"></param>
/// <param name="text"></param>
/// <param name="colorName"></param>
public void AddBox (float xmin, float xmax, float ymin, float ymax, string text = "", string colorName = "red")
{
var left = xmin * _image.Width;
var right = xmax * _image.Width;
var top = ymin * _image.Height;
var bottom = ymax * _image.Height;
var imageRectangle = new Rectangle (new Point (0, 0), new Size (_image.Width, _image.Height));
_graphics.DrawImage (_image, imageRectangle);
Color color = Color.FromName(colorName);
Brush brush = new SolidBrush (color);
Pen pen = new Pen (brush);
_graphics.DrawRectangle (pen, left, top, right - left, bottom - top);
var font = new Font (_fontFamily, _fontSize);
SizeF size = _graphics.MeasureString (text, font);
_graphics.DrawString (text, font, brush, new PointF (left, top - size.Height));
}
public void Dispose ()
{
if (_image != null) {
_image.Save (_outputFile);
if (_graphics != null) {
_graphics.Dispose ();
}
_image.Dispose ();
}
}
}
}