-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogoOptionsControl.cs
More file actions
340 lines (298 loc) · 14 KB
/
LogoOptionsControl.cs
File metadata and controls
340 lines (298 loc) · 14 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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
using System.Globalization;
using System.Diagnostics;
namespace VideoLogoBatch
{
public partial class LogoOptionsControl : UserControl
{
private string? _currentLogoPath = null;
public event EventHandler? LogoSettingsChanged;
private int _logoSizePercent = 50;
private int _logoOpacityPercent = 100;
private int _logoMargin = 10;
public LogoOptionsControl()
{
InitializeComponent();
pic_box_preview_logo.SizeMode = PictureBoxSizeMode.Zoom;
btn_import.Click += Btn_import_Click;
btn_clean.Click += Btn_clean_Click;
comboBox_logo_position.SelectedIndexChanged += ComboBox_logo_position_SelectedIndexChanged;
trackBar_logosize.Scroll += TrackBar_logosize_Scroll;
textBox_logosize.TextChanged += TextBox_logosize_TextChanged;
textBox_logosize.KeyPress += TextBox_Numeric_KeyPress;
trackBar_logo_opacity.Scroll += TrackBar_logo_opacity_Scroll;
textBox_logo_opacity.TextChanged += TextBox_logo_opacity_TextChanged;
textBox_logo_opacity.KeyPress += TextBox_Numeric_KeyPress;
textBox_logo_margin.TextChanged += TextBox_logo_margin_TextChanged;
textBox_logo_margin.KeyPress += TextBox_Numeric_KeyPress;
checkBox_random_position.CheckedChanged += CheckBox_random_position_CheckedChanged;
InitializeDefaultValues();
}
private void InitializeDefaultValues()
{
if (comboBox_logo_position.Items.Contains("Top Center"))
{
comboBox_logo_position.SelectedItem = "Top Center";
}
else if (comboBox_logo_position.Items.Count > 0)
{
comboBox_logo_position.SelectedIndex = 0;
}
trackBar_logosize.Minimum = 1;
trackBar_logosize.Maximum = 100;
_logoSizePercent = Math.Clamp(_logoSizePercent, trackBar_logosize.Minimum, trackBar_logosize.Maximum);
trackBar_logosize.Value = _logoSizePercent;
textBox_logosize.Text = _logoSizePercent.ToString();
trackBar_logo_opacity.Minimum = 0;
trackBar_logo_opacity.Maximum = 100;
_logoOpacityPercent = Math.Clamp(_logoOpacityPercent, trackBar_logo_opacity.Minimum, trackBar_logo_opacity.Maximum);
trackBar_logo_opacity.Value = _logoOpacityPercent;
textBox_logo_opacity.Text = _logoOpacityPercent.ToString();
textBox_logo_margin.Text = _logoMargin.ToString();
}
private Image CreateThumbnail(Image originalImage, int maxWidth, int maxHeight)
{
if (originalImage == null) throw new ArgumentNullException(nameof(originalImage));
if (originalImage.Width <= maxWidth && originalImage.Height <= maxHeight)
{
return new Bitmap(originalImage);
}
float ratioX = (float)maxWidth / originalImage.Width;
float ratioY = (float)maxHeight / originalImage.Height;
float ratio = Math.Min(ratioX, ratioY);
int newWidth = (int)(originalImage.Width * ratio);
int newHeight = (int)(originalImage.Height * ratio);
if (newWidth <= 0) newWidth = 1;
if (newHeight <= 0) newHeight = 1;
PixelFormat originalPixelFormat = originalImage.PixelFormat;
if (originalPixelFormat == PixelFormat.Format1bppIndexed ||
originalPixelFormat == PixelFormat.Format4bppIndexed ||
originalPixelFormat == PixelFormat.Format8bppIndexed ||
(originalImage.Flags & (int)ImageFlags.HasAlpha) == 0)
{
originalPixelFormat = PixelFormat.Format32bppArgb;
}
Bitmap newImage = new Bitmap(newWidth, newHeight, originalPixelFormat);
newImage.SetResolution(originalImage.HorizontalResolution, originalImage.VerticalResolution);
using (Graphics g = Graphics.FromImage(newImage))
{
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.HighQuality;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.CompositingQuality = CompositingQuality.HighQuality;
g.DrawImage(originalImage, 0, 0, newWidth, newHeight);
}
return newImage;
}
private void Btn_import_Click(object? sender, EventArgs e)
{
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.Title = "Chọn ảnh Logo";
openFileDialog.Filter = "Image Files|*.png;*.jpg;*.jpeg;*.bmp;*.gif|PNG files (*.png)|*.png|JPEG files (*.jpg;*.jpeg)|*.jpg;*.jpeg|All files (*.*)|*.*";
openFileDialog.FilterIndex = 1;
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string selectedFilePath = openFileDialog.FileName;
Image? oldImageInPictureBox = pic_box_preview_logo.Image;
pic_box_preview_logo.Image = null;
try
{
using (Image originalImage = Image.FromFile(selectedFilePath))
{
Image thumbnail = CreateThumbnail(originalImage, pic_box_preview_logo.Width, pic_box_preview_logo.Height);
pic_box_preview_logo.Image = thumbnail;
}
_currentLogoPath = selectedFilePath;
OnLogoSettingsChanged();
}
catch (OutOfMemoryException memEx)
{
MessageBox.Show($"Lỗi hết bộ nhớ khi mở tệp ảnh. Tệp có thể quá lớn hoặc bị lỗi.\nChi tiết: {memEx.Message}", "Lỗi Ảnh Logo - OutOfMemory", MessageBoxButtons.OK, MessageBoxIcon.Error);
pic_box_preview_logo.Image = oldImageInPictureBox;
oldImageInPictureBox = null;
_currentLogoPath = null;
}
catch (Exception ex)
{
MessageBox.Show($"Lỗi khi mở tệp ảnh: {ex.Message}", "Lỗi Ảnh Logo", MessageBoxButtons.OK, MessageBoxIcon.Error);
pic_box_preview_logo.Image = oldImageInPictureBox;
oldImageInPictureBox = null;
_currentLogoPath = null;
}
finally
{
oldImageInPictureBox?.Dispose();
}
}
}
}
private void Btn_clean_Click(object? sender, EventArgs e)
{
pic_box_preview_logo.Image?.Dispose();
pic_box_preview_logo.Image = null;
_currentLogoPath = null;
OnLogoSettingsChanged();
}
private void ComboBox_logo_position_SelectedIndexChanged(object? sender, EventArgs e)
{
bool isCenterPosition = comboBox_logo_position.SelectedItem?.ToString() == "Center";
textBox_logo_margin.Enabled = !isCenterPosition && !checkBox_random_position.Checked;
OnLogoSettingsChanged();
}
private bool _isUpdatingTextBoxSize = false;
private void TrackBar_logosize_Scroll(object? sender, EventArgs e)
{
if (textBox_logosize.Text != trackBar_logosize.Value.ToString())
{
_logoSizePercent = trackBar_logosize.Value;
_isUpdatingTextBoxSize = true;
textBox_logosize.Text = _logoSizePercent.ToString();
_isUpdatingTextBoxSize = false;
OnLogoSettingsChanged();
}
}
private void TextBox_logosize_TextChanged(object? sender, EventArgs e)
{
if (_isUpdatingTextBoxSize) return;
if (int.TryParse(textBox_logosize.Text, out int value))
{
value = Math.Clamp(value, trackBar_logosize.Minimum, trackBar_logosize.Maximum);
if (trackBar_logosize.Value != value)
{
trackBar_logosize.Value = value;
}
if (_logoSizePercent != value)
{
_logoSizePercent = value;
OnLogoSettingsChanged();
}
if (textBox_logosize.Text != value.ToString())
{
_isUpdatingTextBoxSize = true;
textBox_logosize.Text = value.ToString();
textBox_logosize.SelectionStart = textBox_logosize.Text.Length;
_isUpdatingTextBoxSize = false;
}
}
}
private bool _isUpdatingTextBoxOpacity = false;
private void TrackBar_logo_opacity_Scroll(object? sender, EventArgs e)
{
if (textBox_logo_opacity.Text != trackBar_logo_opacity.Value.ToString())
{
_logoOpacityPercent = trackBar_logo_opacity.Value;
_isUpdatingTextBoxOpacity = true;
textBox_logo_opacity.Text = _logoOpacityPercent.ToString();
_isUpdatingTextBoxOpacity = false;
OnLogoSettingsChanged();
}
}
private void TextBox_logo_opacity_TextChanged(object? sender, EventArgs e)
{
if (_isUpdatingTextBoxOpacity) return;
if (int.TryParse(textBox_logo_opacity.Text, out int value))
{
value = Math.Clamp(value, trackBar_logo_opacity.Minimum, trackBar_logo_opacity.Maximum);
if (trackBar_logo_opacity.Value != value)
{
trackBar_logo_opacity.Value = value;
}
if (_logoOpacityPercent != value)
{
_logoOpacityPercent = value;
OnLogoSettingsChanged();
}
if (textBox_logo_opacity.Text != value.ToString())
{
_isUpdatingTextBoxOpacity = true;
textBox_logo_opacity.Text = value.ToString();
textBox_logo_opacity.SelectionStart = textBox_logo_opacity.Text.Length;
_isUpdatingTextBoxOpacity = false;
}
}
}
private bool _isUpdatingTextBoxMargin = false;
private void TextBox_logo_margin_TextChanged(object? sender, EventArgs e)
{
if (_isUpdatingTextBoxMargin) return;
if (int.TryParse(textBox_logo_margin.Text, out int value))
{
value = Math.Max(0, value);
if (_logoMargin != value)
{
_logoMargin = value;
OnLogoSettingsChanged();
}
if (textBox_logo_margin.Text != value.ToString())
{
_isUpdatingTextBoxMargin = true;
textBox_logo_margin.Text = value.ToString();
textBox_logo_margin.SelectionStart = textBox_logo_margin.Text.Length;
_isUpdatingTextBoxMargin = false;
}
}
}
private void CheckBox_random_position_CheckedChanged(object? sender, EventArgs e)
{
bool isFloatingLogoEnabled = checkBox_random_position.Checked;
comboBox_logo_position.Enabled = !isFloatingLogoEnabled;
bool isCenterPositionStatic = comboBox_logo_position.SelectedItem?.ToString() == "Center";
textBox_logo_margin.Enabled = !isFloatingLogoEnabled || (!isCenterPositionStatic && !isFloatingLogoEnabled);
OnLogoSettingsChanged();
}
private void TextBox_Numeric_KeyPress(object? sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
protected virtual void OnLogoSettingsChanged()
{
LogoSettingsChanged?.Invoke(this, EventArgs.Empty);
}
public string? GetCurrentLogoPath() => _currentLogoPath;
public string GetLogoPosition() => comboBox_logo_position.SelectedItem?.ToString() ?? (comboBox_logo_position.Items.Count > 0 ? (comboBox_logo_position.Items[0]?.ToString()) ?? "Top Left" : "Top Left");
public int GetLogoSizePercent() => _logoSizePercent;
public int GetLogoOpacityPercent() => _logoOpacityPercent;
public int GetLogoMargin() => _logoMargin;
public bool IsRandomPositionEnabled() => checkBox_random_position.Checked;
public class LogoSettingsData
{
public string? Path { get; set; }
public string Position { get; set; } = "Top Center";
public int SizePercent { get; set; } = 50;
public int OpacityPercent { get; set; } = 100;
public int Margin { get; set; } = 10;
public bool IsRandomPosition { get; set; } = false;
}
public LogoSettingsData GetCurrentLogoSettings()
{
string position = "Top Left";
if (comboBox_logo_position.SelectedItem != null)
{
position = comboBox_logo_position.SelectedItem.ToString() ?? position;
}
else if (comboBox_logo_position.Items.Count > 0)
{
position = (comboBox_logo_position.Items[0]?.ToString()) ?? position;
}
return new LogoSettingsData
{
Path = _currentLogoPath,
Position = position,
SizePercent = _logoSizePercent,
OpacityPercent = _logoOpacityPercent,
Margin = _logoMargin,
IsRandomPosition = checkBox_random_position.Checked
};
}
}
}