-
Notifications
You must be signed in to change notification settings - Fork 0
/
DualImagePanel.cs
302 lines (253 loc) · 9.52 KB
/
DualImagePanel.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace JpegRotator
{
public class DualImagePanel : Panel
{
private Orientation _orientation = Orientation.Initial;
private PictureBox _originalImagePictureBox;
private PictureBox _rotatedImagePictureBox;
private Label _filenameLabel;
private string _filename;
//Could make this more efficient by just instantiating one copy of this bitmap
//for the entire application, not one for every DualImagePanel.
private Bitmap _arrowBitmap;
#region Raised Events
public delegate void RotationDelegate(object sender, RotationEventArgs e);
public event RotationDelegate RotationEvent;
#endregion
#region Construction
/// <summary>
/// This class is a Panel that displays two images. The one on the left is the "original"
/// image, a thumbnail view of an image as it currently is on the disk. On the right is the
/// "rotated" image, an rotated version of the original image.
/// </summary>
/// <param name="thumbnailImageWidth">The maximum width of the thumbnail images. </param>
/// <param name="thumbnailImageHeight">The maximum height of the thumbnail images. </param>
public DualImagePanel(int thumbnailImageWidth, int thumbnailImageHeight)
{
this._originalImagePictureBox = new PictureBox();
this._originalImagePictureBox.Location = new Point(10, 10);
this._originalImagePictureBox.Size = new Size(thumbnailImageWidth, thumbnailImageHeight);
this._originalImagePictureBox.MouseClick += new MouseEventHandler(PictureBox_MouseClick);
this.Controls.Add(this._originalImagePictureBox);
this._rotatedImagePictureBox = new PictureBox();
this._rotatedImagePictureBox.Location = new Point(45 + thumbnailImageWidth, 10);
this._rotatedImagePictureBox.Size = new Size(thumbnailImageWidth, thumbnailImageHeight);
this._rotatedImagePictureBox.MouseClick += new MouseEventHandler(PictureBox_MouseClick);
this.Controls.Add(this._rotatedImagePictureBox);
this._filenameLabel = new Label();
this._filenameLabel.Size = new Size(300, 50);
this._filenameLabel.Location = new Point(0, 155);
//this._filenameLabel.Font = new Font(this._filenameLabel.Font, FontStyle.Bold);
this.Controls.Add(this._filenameLabel);
this._arrowBitmap = new Bitmap(typeof(DualImagePanel), "Arrow3.bmp");
}
#endregion
#region Public Methods
/// <summary>
/// Sets the "Original Image" for this DualImagePanel, which is the thumbnail version
/// of the image as it is on the disk (i.e. with no rotation applied).
/// </summary>
/// <param name="originalImage">The non-rotated thumbnail image. </param>
/// <param name="orientation">The initial orientation of the large version of the
/// specified image. </param>
/// <param name="filename">The fully-specified (including path) filename of the large
/// version of the image. </param>
public void SetOriginalImage(Image originalImageThumbnail, Orientation orientation, string filename)
{
this._originalImagePictureBox.Image = originalImageThumbnail;
//At this point, we want to calculate the "correction" for this image, to get the
//rotated image to (hopefully, if the EXIF data we got was accurate) appear upright.
switch (orientation)
{
case Orientation.Initial:
//No action necessary.
this._orientation = Orientation.Initial;
break;
case Orientation.Left:
//The original image is rotated counterclockwise, so we need to rotate
//clockwise to get it to appear upright.
this._orientation = Orientation.Right;
break;
case Orientation.Right:
//The original image is rotated clockwise, so we need to rotate
//counterclockwise to get it to appear upright.
this._orientation = Orientation.Left;
break;
case Orientation.Inverted:
//The original image is inverted (rotated 180 degrees), so we invert
//it again to get it to appear upright.
this._orientation = Orientation.Inverted;
break;
default:
throw new ApplicationException("Unrecognized Orientation value: " + orientation);
}
this._filename = filename;
//Get the name of the file with the filename only, not the fully specified path.
string filenameWithNoPath = System.IO.Path.GetFileName(filename);
this._filenameLabel.Text = filenameWithNoPath;
UpdateRotatedImage();
UpdateColor();
}
/// <summary>
/// Sets the original image to be the rotated image, and sets the orientation to Initial.
/// This should be called after the rotated image has been saved to disk, replacing the
/// original image.
/// </summary>
public void SetRotatedImageAsOriginalImage()
{
this._originalImagePictureBox.Image = this._rotatedImagePictureBox.Image;
this._orientation = Orientation.Initial;
UpdateColor();
}
/// <summary>
/// Gets the current orientation of the image in this DualImagePanel.
/// </summary>
public Orientation Orientation
{
get
{
return this._orientation;
}
}
/// <summary>
/// Gets the fully-specified Filename of the image in this DualImagePanel.
/// </summary>
public string Filename
{
get
{
return this._filename;
}
}
#endregion
#region Event Handlers
/// <summary>
/// Activated when one of the ImagePanels on this DualImagePanel is clicked.
/// </summary>
/// <param name="sender">The clicked ImagePanel. </param>
/// <param name="e">Information about the click event. </param>
private void PictureBox_MouseClick(object sender, MouseEventArgs e)
{
HandleMouseClick(e);
}
/// <summary>
/// Activated when this DualImagePanel is clicked.
/// </summary>
/// <param name="e">Information about the click event. </param>
protected override void OnMouseClick(MouseEventArgs e)
{
HandleMouseClick(e);
}
/// <summary>
/// Activated when this DualImagePanel paints. Does the default base class
/// paint, then draws an arrow between the original image and the rotated image
/// if the current orientation is not the initial orientation.
/// </summary>
/// <param name="e"></param>
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (this._orientation != Orientation.Initial)
{
e.Graphics.DrawImage(_arrowBitmap, 160, 65);
}
}
#endregion
#region Private Methods
/// <summary>
/// Sets the "Rotated Image" for this DualImagePanel, which is the thumbnail version
/// of the rotated version of the image.
/// </summary>
private void UpdateRotatedImage()
{
//Create a rotated version of the image.
Image originalImage = this._originalImagePictureBox.Image;
Image rotatedImage = Utility.RotateImage(originalImage, this._orientation);
this._rotatedImagePictureBox.Image = rotatedImage;
}
/// <summary>
/// Rotates the image in this DualImagePanel. Direction of rotation is determined
/// by which mouse button was clicked.
/// </summary>
/// <param name="e">The information about the click event. </param>
private void HandleMouseClick(MouseEventArgs e)
{
//Rotate left (counterclockwise) on a left click or right (clockwise) on a
//right click. Ignore other button clicks.
if (e.Button == MouseButtons.Left)
{
this.RotateCounterclockwise();
}
else if (e.Button == MouseButtons.Right)
{
this.RotateClockwise();
}
}
/// <summary>
/// Rotates the image counterclockwise (left).
/// </summary>
private void RotateCounterclockwise()
{
//Save the initial (pre-rotation) orienatation to use when we fire an event below.
Orientation originalOrientation = this._orientation;
//Update the orientation flag for this DualImagePanel.
this._orientation = (Orientation)((((int)this._orientation) + 1) % 4);
//Update the rotated image thumbnail to match the new orientation.
UpdateRotatedImage();
//Update the color of the control.
UpdateColor();
//Fire an event to let the main form know that an image was rotated.
FireRotationEvent(originalOrientation, this._orientation);
}
/// <summary>
/// Rotates the image clockwise (right).
/// </summary>
private void RotateClockwise()
{
//Save the initial (pre-rotation) orienatation to use when we fire an event below.
Orientation originalOrientation = this._orientation;
//Update the orientation flag for this DualImagePanel.
this._orientation = (Orientation)((((int)this._orientation) + 3) % 4);
//Update the rotated image thumbnail to match the new orientation.
UpdateRotatedImage();
//Update the color of the control.
UpdateColor();
//Fire an event to let the main form know that an image was rotated.
FireRotationEvent(originalOrientation, this._orientation);
}
/// <summary>
/// Updates the background color of this DualImagePanel based on the current
/// orientation of the rotated image.
/// </summary>
private void UpdateColor()
{
if (this._orientation == Orientation.Initial)
{
this.BackColor = Color.LightGray;
}
else
{
this.BackColor = Color.LightBlue;
}
}
/// <summary>
/// Fires a RotationEvent to notify the main form that the image has been rotated.
/// </summary>
/// <param name="originalOrientation">The original (pre-rotation) orientation. </param>
/// <param name="newOrientation">The new orientation. </param>
private void FireRotationEvent(Orientation originalOrientation, Orientation newOrientation)
{
if (this.RotationEvent != null)
{
RotationEventArgs rotationEventArgs = new RotationEventArgs(originalOrientation, newOrientation);
this.RotationEvent(this, rotationEventArgs);
}
}
#endregion
}
}