-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageCardControl.cs
More file actions
211 lines (181 loc) · 6.24 KB
/
Copy pathImageCardControl.cs
File metadata and controls
211 lines (181 loc) · 6.24 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
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
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace DropResize
{
public class ImageCardControl : UserControl
{
private readonly PictureBox thumbnailBox;
private readonly Label titleLabel;
private readonly Label detailsLabel;
private readonly Label statusLabel;
private bool isSelected;
private bool isKeyboardFocused;
public ImageCardControl(ResizeResult result)
{
Result = result;
Width = 244;
Height = 132;
Margin = new Padding(0, 0, 8, 8);
BackColor = Color.White;
thumbnailBox = new PictureBox
{
Location = new Point(10, 10),
Size = new Size(72, 72),
SizeMode = PictureBoxSizeMode.Zoom,
BackColor = Color.FromArgb(242, 244, 246)
};
titleLabel = new Label
{
Location = new Point(90, 8),
Size = new Size(142, 28),
Font = new Font(Font.FontFamily, 8.0f, FontStyle.Bold),
AutoEllipsis = true
};
detailsLabel = new Label
{
Location = new Point(90, 38),
Size = new Size(142, 62),
Font = new Font(Font.FontFamily, 7.6f),
ForeColor = Color.FromArgb(55, 63, 70)
};
statusLabel = new Label
{
Location = new Point(10, 100),
Size = new Size(222, 22),
Font = new Font(Font.FontFamily, 7.6f, FontStyle.Bold),
ForeColor = result.Success ? Color.FromArgb(20, 120, 70) : Color.FromArgb(180, 45, 45),
AutoEllipsis = true
};
Controls.Add(thumbnailBox);
Controls.Add(titleLabel);
Controls.Add(detailsLabel);
Controls.Add(statusLabel);
Populate();
WireMouseEvents(this);
}
public event MouseEventHandler CardMouseDown;
public event MouseEventHandler CardMouseMove;
public event MouseEventHandler CardMouseUp;
public ResizeResult Result { get; private set; }
public bool IsSelected
{
get { return isSelected; }
set
{
isSelected = value;
BackColor = isSelected ? Color.FromArgb(229, 243, 255) : Color.White;
}
}
public bool IsKeyboardFocused
{
get { return isKeyboardFocused; }
set
{
isKeyboardFocused = value;
Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var color = isKeyboardFocused ? Color.FromArgb(0, 120, 215) : Color.FromArgb(180, 180, 180);
var width = isKeyboardFocused ? 2 : 1;
using (var pen = new Pen(color, width))
{
var offset = width / 2;
e.Graphics.DrawRectangle(
pen,
offset,
offset,
Width - width,
Height - width);
}
}
protected override void Dispose(bool disposing)
{
if (disposing && thumbnailBox.Image != null)
{
thumbnailBox.Image.Dispose();
}
base.Dispose(disposing);
}
public void SetDropHandlers(DragEventHandler dragEnter, DragEventHandler dragDrop)
{
WireDropEvents(this, dragEnter, dragDrop);
}
private void Populate()
{
titleLabel.Text = Path.GetFileName(Result.InputPath);
if (Result.Success)
{
thumbnailBox.Image = Result.Thumbnail ?? LoadThumbnail(Result.OutputPath);
}
detailsLabel.Text =
"Original: " + Result.OriginalWidth + " x " + Result.OriginalHeight + Environment.NewLine +
"Output: " + Result.OutputWidth + " x " + Result.OutputHeight + Environment.NewLine +
"Input: " + FormatBytes(Result.OriginalFileSize) + Environment.NewLine +
"Output: " + FormatBytes(Result.OutputFileSize);
statusLabel.Text = Result.Success ? "Done" : "Error: " + Result.ErrorMessage;
}
private static Image LoadThumbnail(string path)
{
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var image = Image.FromStream(stream))
{
return new Bitmap(image);
}
}
private static string FormatBytes(long bytes)
{
if (bytes < 1024)
{
return bytes + " B";
}
if (bytes < 1024 * 1024)
{
return (bytes / 1024.0).ToString("0.0") + " KB";
}
return (bytes / 1024.0 / 1024.0).ToString("0.0") + " MB";
}
private void WireMouseEvents(Control control)
{
control.MouseDown += (sender, args) =>
{
if (CardMouseDown != null)
{
CardMouseDown(this, args);
}
};
control.MouseMove += (sender, args) =>
{
if (CardMouseMove != null)
{
CardMouseMove(this, args);
}
};
control.MouseUp += (sender, args) =>
{
if (CardMouseUp != null)
{
CardMouseUp(this, args);
}
};
foreach (Control child in control.Controls)
{
WireMouseEvents(child);
}
}
private static void WireDropEvents(Control control, DragEventHandler dragEnter, DragEventHandler dragDrop)
{
control.AllowDrop = true;
control.DragEnter += dragEnter;
control.DragDrop += dragDrop;
foreach (Control child in control.Controls)
{
WireDropEvents(child, dragEnter, dragDrop);
}
}
}
}