-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrmMain.cs
341 lines (292 loc) · 11.9 KB
/
frmMain.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
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
341
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ImageCaptionEdit
{
public partial class frmMain : Form
{
public frmMain(string[] Args)
{
InitializeComponent();
FileListBox.ContextMenu = contextMenu1;
this.Text = "Image Caption Edit - v" + Application.ProductVersion;
if (Args.Length != 0)
{
SelectFile(Args[0]);
}
}
public System.Collections.ArrayList FileNameList = new System.Collections.ArrayList();
public string CurrentFileName;
string CurrentTempFile;
public int FileIndex;
private bool dirty;
private void OpenFileButton_Click(object sender, EventArgs e)
{
openFileDialog1.FileName = "";
openFileDialog1.Filter = "Image Files |*.png;*.jpg;*.tif;*.bmp;*.pcx";
openFileDialog1.ShowDialog();
//MessageBox.Show(openFileDialog1.FileName);
if (!System.IO.File.Exists(openFileDialog1.FileName)) return;
SelectFile(openFileDialog1.FileName);
}
public void FileListViewPopulate()
{
System.IO.FileInfo f = new System.IO.FileInfo(CurrentFileName);
System.IO.DirectoryInfo dir;
if (System.IO.File.Exists(CurrentFileName))
{
//file exists, so select containing directory
dir = new System.IO.DirectoryInfo(f.Directory.FullName);
}
else
{
//check if we're entering a directory..
if (!System.IO.Directory.Exists(CurrentFileName))
{
return;
}
dir = new System.IO.DirectoryInfo(CurrentFileName);
}
string[] FileTypes = new string[5] { "*.jpg", "*.png", "*.tif", "*.bmp", "*.pcx" };
foreach (string type in FileTypes)
{
if (FileTypeCombobox.SelectedIndex != 1 || type == "*.jpg")
{
foreach (System.IO.FileInfo fi in dir.GetFiles(type))
{
FileListBox.Items.Add(fi.Name + " " + fi.CreationTime);
FileNameList.Add(fi.FullName);
//FileListBox.Items.
}
}
}
for (int i = 0; i < FileNameList.Count; i++)
{
if (FileNameList[i].ToString() == CurrentFileName)
{
FileIndex = i;
FileListBox.SelectedIndex = i;
}
}
}
public void SelectFile(string filename)
{
if (dirty == true)
{
DialogResult result;
result = MessageBox.Show("Do you want to save changes to \"" + CurrentFileName + "\"?","Image Caption Edit",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Exclamation,MessageBoxDefaultButton.Button1);
if (result == DialogResult.Yes)
{
SaveButton_Click(this,EventArgs.Empty);
}
else if (result == DialogResult.Cancel) return;
}
if (filename == "") return;
System.IO.FileInfo f = new System.IO.FileInfo(filename);
if (f.Exists)
{
CurrentTempFile = System.IO.Path.GetTempPath() + "__ImageCaptionEdit__" + System.IO.Path.GetRandomFileName();
//load the image
Image theImage = new Bitmap(filename);
//save it as a .jpg
theImage.Save(CurrentTempFile, System.Drawing.Imaging.ImageFormat.Jpeg);
//close it
theImage.Dispose();
//System.IO.File.Copy(filename, CurrentTempFile,true);
pictureBox1.Load(CurrentTempFile);
EXIFData.GetData(CurrentTempFile);
DisplayData();
FNameTxtBox.Text = f.Directory.FullName;
}
else
{
//check if we're entering a directory..
if (!System.IO.Directory.Exists(filename))
{
return;
}
FNameTxtBox.Text = filename;
}
dirty = false;
FileListBox.Items.Clear();
FileNameList.Clear();
CurrentFileName = filename;
SetTitle();
FileListViewPopulate();
}
private void DisplayData()
{
HeaderTextBox.Text = EXIFData.header;
SideTextBox.Text = EXIFData.sideinfo;
CaptionTextBox.Text = EXIFData.description;
}
private void SaveButton_Click(object sender, EventArgs e)
{
if (!System.IO.File.Exists(CurrentFileName)) return;
EXIFData.header = HeaderTextBox.Text;
EXIFData.sideinfo = SideTextBox.Text;
EXIFData.description = CaptionTextBox.Text;
string filename = CurrentFileName;
System.IO.FileInfo f = new System.IO.FileInfo(CurrentFileName);
if (f.Extension.ToUpper() != ".jpg".ToUpper())
{
filename = f.FullName.TrimEnd(f.Extension.ToCharArray());
filename = filename + ".jpg";
if (System.IO.File.Exists(filename))
{
if (MessageBox.Show("File: " + filename + " already exists. Overwite?", "Overwite?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) != DialogResult.Yes)
{
return;
}
}
}
EXIFData.SaveData(CurrentTempFile,filename);
dirty = false;
SetTitle();
FileListViewPopulate();
}
private void NextButton_Click(object sender, EventArgs e)
{
for (int i = 0; i < FileNameList.Count; i++)
{
if (FileNameList[i].ToString() == CurrentFileName)
{
if (i >= FileNameList.Count - 1) i = -1;
SelectFile(FileNameList[i + 1].ToString());
return;
}
}
}
private void PrevButton_Click(object sender, EventArgs e)
{
for (int i = 0; i < FileNameList.Count; i++)
{
if (FileNameList[i].ToString() == CurrentFileName)
{
if (i == 0) i = FileNameList.Count;
SelectFile(FileNameList[i - 1].ToString());
return;
}
}
}
private void FileListBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (FileIndex == FileListBox.SelectedIndex) return;
int SelIndex;
SelIndex = FileListBox.SelectedIndex;
if (FileListBox.SelectedIndex >= FileListBox.Items.Count ||
FileListBox.SelectedIndex < 0)
{
SelIndex = FileListBox.Items.Count - 1;
}
//MessageBox.Show(FileListBox.SelectedIndex.ToString());
SelectFile(FileNameList[SelIndex].ToString());
}
private void FNameTxtBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode != Keys.Enter) return;
SelectFile(FNameTxtBox.Text);
}
private void PrintPreviewToolStripMenuItem_Click(object sender, EventArgs e)
{
string TempFile;
//MessageBox.Show("Print Preview..");
TempFile = System.IO.Path.GetTempPath() + "__ImageCaptionEdit__" + System.IO.Path.GetRandomFileName();
System.IO.File.Copy(CurrentFileName, TempFile);
if (!System.IO.File.Exists(CurrentFileName)) return;
EXIFData.header = HeaderTextBox.Text;
EXIFData.sideinfo = SideTextBox.Text;
EXIFData.description = CaptionTextBox.Text;
EXIFData.SaveData(CurrentTempFile, TempFile); //save current file to the temp file
PrintPreviewMine.TempFileName = TempFile;
PrintPreviewMine.FileName = CurrentFileName;
PrintPreviewMine preview = new PrintPreviewMine();
preview.Show();
}
private void PrintButton_Click(object sender, EventArgs e)
{
PrintPreviewMine.TempFileName = CurrentTempFile;
PrintPreviewMine.FileName = CurrentFileName;
PrintPreviewMine preview = new PrintPreviewMine();
preview.PrintImage();
}
string backupHeader;
string backupSide;
string backupCaption;
private void CopyAllFieldsButton_Click(object sender, EventArgs e)
{
backupHeader = HeaderTextBox.Text;
backupSide = SideTextBox.Text;
backupCaption = CaptionTextBox.Text;
}
private void PasteAllFieldsButton_Click(object sender, EventArgs e)
{
HeaderTextBox.Text = backupHeader;
SideTextBox.Text = backupSide;
CaptionTextBox.Text = backupCaption;
HeaderTextBox.Focus();
}
private void TextBox_TextChanged(object sender, EventArgs e)
{
dirty = true;
SetTitle();
}
private void SetTitle()
{
string displayname;
if (CurrentFileName == null) return;
System.IO.FileInfo fi = new System.IO.FileInfo(CurrentFileName);
displayname = fi.Name;
if (displayname.Length > 40)
{
displayname = displayname.Remove(40);
displayname += "...";
}
if (dirty) displayname += "*";
this.Text = "Image Caption Edit - " + displayname;
}
private void FileTypeCombobox_SelectedIndexChanged(object sender, EventArgs e)
{
SelectFile(FNameTxtBox.Text);
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void menuItem1_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Print all selected files?", "Print selection", MessageBoxButtons.YesNo) == DialogResult.No) return;
//foreach (int index in FileListBox.SelectedItems.IndexOf(FileListBox))
//{
// MessageBox.Show(FileListBox.Items[index].ToString);
//}
//ListBox.SelectedObjectCollection SelectedItems = FileListBox.SelectedItems;
for (int i = 0; i < FileListBox.Items.Count; i++)
{
if (FileListBox.SelectedItems.Contains(FileListBox.Items[i]))
{
//MessageBox.Show(FileListBox.Items[i].ToString());
//MessageBox.Show(FileNameList[i].ToString());
PrintPreviewMine.TempFileName = System.IO.Path.GetTempPath() + "__ImageCaptionEdit__" + System.IO.Path.GetRandomFileName();
//load the image
Image theImage = new Bitmap(FileNameList[i].ToString());
//save it as a .jpg in a temporary file
theImage.Save(PrintPreviewMine.TempFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
//close it
theImage.Dispose();
PrintPreviewMine.FileName = FileNameList[i].ToString(); //save filename if needed...
PrintPreviewMine preview = new PrintPreviewMine();
preview.PrintImage();
}
}
//foreach (string item in FileListBox.SelectedItems)
//{
// MessageBox.Show(item);
//}
}
}
}