-
Notifications
You must be signed in to change notification settings - Fork 1
/
Progress.cs
201 lines (164 loc) · 6.33 KB
/
Progress.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace certificate_generator
{
public partial class Progress : Form
{
List<Tuple<List<Tuple<string, Point, Font, Color>>, string>> dic;
string img_path;
string extension;
string output_folder_path;
string msg;
public Progress(List<Tuple<List<Tuple<string, Point, Font, Color>>, string>> dic, string img_path, string extension, string output_folder_path)
{
InitializeComponent();
this.dic = dic;
this.img_path = img_path;
this.extension = extension;
this.output_folder_path = output_folder_path;
}
private void Progress_Load(object sender, EventArgs e)
{
feed_back_lbl.Text = "Working...";
bar.Maximum = dic.Count;
bar.Value = 0;
finish_btn.Enabled = false;
bg_worker.RunWorkerAsync();
}
private void bg_worker_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
int progress = 0;
worker.ReportProgress(progress);
msg = "Reading Files...";
worker.ReportProgress(progress);
foreach (var tpl1 in dic)
{
if (worker.CancellationPending)
{
e.Cancel = true;
break;
}
//string img_path = tmplt_path_tb.Text;
FileStream fs = new FileStream(img_path, FileMode.Open, FileAccess.Read);
Image image = Image.FromStream(fs);
fs.Close();
Bitmap b = new Bitmap(image);
Graphics graphics = Graphics.FromImage(b);
msg = "Writing Text on files...";
worker.ReportProgress(progress);
foreach (Tuple<string, Point, Font, Color> tpl in tpl1.Item1)
{
Brush brush = new SolidBrush(Color.FromName(tpl.Item4.Name));
if (!tpl.Item4.IsNamedColor)
{
MessageBox.Show("Not Valid color : " + tpl.Item4.Name);
brush = new SolidBrush(Color.FromName("Black"));
}
graphics.DrawString(tpl.Item1, tpl.Item3, brush, tpl.Item2.X, tpl.Item2.Y);
}
msg = "Image " + tpl1.Item2 + " saved";
worker.ReportProgress(progress);
//string extension = get_output_file_type();
// ==== SAVING IMAGE FILE =========
string output_path = output_folder_path + "\\" + tpl1.Item2;//without extension
if (extension == ".pdf")
{
msg = "Converting to PDF file...";
worker.ReportProgress(progress);
b.Save(output_path + ".png", image.RawFormat);
image.Dispose();
b.Dispose();
convert_to_pdf(output_path + ".png", output_path);
try
{
// Check if file exists with its full path
if (File.Exists(output_path + ".png"))
{
// If file found, delete it
File.Delete(output_path + ".png");
Console.WriteLine(tpl1.Item2 + " deleted.");
}
else
{
MessageBox.Show("File not found");
}
}
catch (IOException ioExp)
{
MessageBox.Show(ioExp.Message);
}
}
else
{
b.Save(output_path + extension, image.RawFormat);
image.Dispose();
b.Dispose();
}
progress += 1;
worker.ReportProgress(progress);
}
// work finished
//File.Delete(img_path);
}
private void bg_worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
bar.Value = e.ProgressPercentage;
progress_lbl.Text = e.ProgressPercentage + "/" + dic.Count;
feed_back_lbl.Text = msg;
Console.WriteLine(e.ProgressPercentage+" % Progress...");
}
private void convert_to_pdf(string img_path, string pdf_path)
{
iTextSharp.text.Rectangle pageSize = null;
using (var srcImage = new Bitmap(img_path))
{
pageSize = new iTextSharp.text.Rectangle(0, 0, srcImage.Width, srcImage.Height);
}
using (var ms = new MemoryStream())
{
var document = new iTextSharp.text.Document(pageSize, 0, 0, 0, 0);
iTextSharp.text.pdf.PdfWriter.GetInstance(document, ms).SetFullCompression();
document.Open();
var pdf_img = iTextSharp.text.Image.GetInstance(img_path);
document.Add(pdf_img);
document.Close();
File.WriteAllBytes(pdf_path + ".pdf", ms.ToArray());
}
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void cncl_btn_Click(object sender, EventArgs e)
{
this.bg_worker.CancelAsync();
cncl_btn.Enabled = false;
}
private void bg_worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
MessageBox.Show(e.Error.Message);
}
else if (e.Cancelled)
{
finish_btn.Enabled = true;
feed_back_lbl.Text = "Canceled";
}
else
{
feed_back_lbl.Text = "Completed !!";
finish_btn.Enabled = true;
}
}
}
}