-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPictureViewBox.cs
196 lines (152 loc) · 5.06 KB
/
PictureViewBox.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
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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PhotoPrinter
{
public delegate void PictureViewBoxSelectionChangedEventHandler(PictureViewBox sender);
public class PictureViewBox : Panel
{
#region Data
private string m_filename;
private Image m_thumbnail;
private bool m_isSelected;
public Font DefaultTextFont { get; private set; }
#endregion Data
#region Properties
public Image Image
{
get { return m_filename != null ? Image.FromFile(m_filename) : null; }
}
public bool IsSelected
{
get { return m_isSelected; }
}
#endregion Properties
#region Events
public event PictureViewBoxSelectionChangedEventHandler SelectionChangeRequested;
private void OnSelectionChangeRequested()
{
if (SelectionChangeRequested != null)
{
SelectionChangeRequested(this);
}
}
#endregion Events
#region Constructors
private PictureViewBox()
{
DefaultTextFont = new Font(FontFamily.GenericSerif, 10, FontStyle.Regular);
m_isSelected = false;
this.Size = new Size(170, 170);
this.BackColor = Color.White;
this.BorderStyle = BorderStyle.FixedSingle;
this.MouseClick += new MouseEventHandler((object sender, MouseEventArgs e) =>
{
if (e.Button == MouseButtons.Left)
{
OnSelectionChangeRequested();
}
});
AttachContextMenu();
}
#endregion Constructors
#region Public Methods
public void Select(bool isSelected)
{
if (isSelected)
{
this.BackColor = Color.Blue;
m_isSelected = true;
}
else
{
this.BackColor = Color.White;
m_isSelected = false;
}
}
public bool RotateFlip(RotateFlipType rotateFlipOption)
{
bool result = false;
if (File.Exists(m_filename))
{
Image image = this.Image;
image.RotateFlip(rotateFlipOption);
image.Save(m_filename);
image.Dispose();
m_thumbnail.RotateFlip(rotateFlipOption);
this.Refresh();
result = true;
}
return result;
}
public static PictureViewBox Create(string filePath)
{
PictureViewBox pvb = new PictureViewBox();
pvb.LoadPictureAsync(filePath);
return pvb;
}
#endregion Public Methods
#region Internals
private void AttachContextMenu()
{
ContextMenu contextMenu = new ContextMenu();
MenuItem miRotate = new MenuItem();
miRotate.Text = "&Rotate";
miRotate.Click += new EventHandler((object s, EventArgs ea) =>
{
this.RotateFlip(RotateFlipType.Rotate90FlipNone);
});
contextMenu.MenuItems.Add(miRotate);
this.ContextMenu = contextMenu;
}
private Task LoadPictureAsync(string filename)
{
return Task.Factory.StartNew(() => {
m_filename = filename;
Image image = Image.FromFile(m_filename);
m_thumbnail = image.GetThumbnailImage(150, 150, null, IntPtr.Zero);
image.Dispose();
this.Invoke(new Action(() => {
this.InvokePaint(this, new PaintEventArgs(this.CreateGraphics(), this.DisplayRectangle));
}));
});
}
#region Image edition
public bool AutoCrop()
{
return true;
}
#endregion Image edition
protected override void OnPaint(PaintEventArgs e)
{
if (m_thumbnail != null)
{
e.Graphics.DrawImage(m_thumbnail, 10, 10);
}
string displayText = null;
SizeF displayTextSize;
do
{
if (displayText == null)
{
displayText = Path.GetFileName(m_filename);
}
else
{
displayText = String.Concat(displayText.Substring(0, displayText.Length - 4), "...");
}
displayTextSize = e.Graphics.MeasureString(displayText, DefaultTextFont);
}
while (displayTextSize.Width > this.Width);
e.Graphics.DrawString(displayText, DefaultTextFont, Brushes.Black,
new PointF((this.Width - displayTextSize.Width)/2, this.Height - displayTextSize.Height));
base.OnPaint(e);
}
#endregion Internals
}
}