-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
253 lines (196 loc) · 7.71 KB
/
Form1.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
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;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ColorPaletteGenerator
{
public partial class Form1 : Form
{
private string folderPath;
private Dictionary<Color, int> dict;
//List<Color> pixelList;
public Form1()
{
Form1.CheckForIllegalCrossThreadCalls = false;
InitializeComponent();
}
private void BtnSingleImage_Click(object sender, EventArgs e)
{
dict = new Dictionary<Color, int>();
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png"; // file types, that will be allowed to upload
dialog.Multiselect = false; // allow/deny user to upload more than one file at a time
if (dialog.ShowDialog() == DialogResult.OK) // if user clicked OK
{
folderPath = Path.GetDirectoryName(dialog.FileName);
var t = new Thread(() => pixelRead(dialog));
t.Start();
lblStatus.Text = "Loading...";
}
}
public void pixelRead(OpenFileDialog dialog) {
//pixelList = new List<Color>();
dict = new Dictionary<Color, int>();
Bitmap img = new Bitmap(dialog.FileName);
int pixelCount = 0;
progressBar1.Maximum = img.Width;
pixelCount += img.Width + img.Height;
for (int i = 0; i < img.Width; i++)
{
for (int j = 0; j < img.Height; j++)
{
Color pixel = img.GetPixel(i, j);
if (!dict.Keys.Contains(pixel))
{
dict.Add(pixel, 1);
}
else
{
dict[pixel] += 1;
}
}
progressBar1.Value += 1;
}
progressBar1.Value = progressBar1.Maximum;
lblTotalPixelCount.Text = $"Pixel Count: {pixelCount}";
lblPixelCount.Text = $"Different Pixel Count: {dict.Count}";
lblStatus.Text = "Done";
btnDownload.Enabled = true;
btnSingleImage.Enabled = false;
btnFolder.Enabled = false;
}
private void BtnDownload_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
sb.Append("<div style=\"padding: 5px;\">");
int count = 0;
Dictionary<Color, int> copy = new Dictionary<Color, int>(dict);
List<Color> colorList = new List<Color>();
while (colorList.Count != copy.Count)
{
KeyValuePair<Color, int> top = new KeyValuePair<Color, int>();
foreach (KeyValuePair<Color, int> entry in dict)
{
if (top.Key.IsEmpty)
{
top = entry;
}
else
{
if (entry.Value > top.Value)
{
top = entry;
}
}
}
colorList.Add(top.Key);
dict.Remove(top.Key);
}
foreach (Color color in colorList) {
count += 1;
if (count < 280)
{
sb.Append("<li style=\"float: left; \">");
sb.Append($"<div style=\"display: flex; margin: 3px; width: 50px; height: 50px; background: rgba({color.R}, {color.G}, {color.B}, {color.A}) ;\"></div><p style=\"text-align: center;\">{copy[color]}</p>");
sb.Append("</li>");
}
else {
sb.Append("<p style=\"page -break-before: always\">");
sb.Append("<li style=\"float: left; \">");
sb.Append($"<div style=\"display: flex; margin: 3px; width: 50px; height: 50px; background: rgba({color.R}, {color.G}, {color.B}, {color.A}) ;\"></div>");
sb.Append("</li>");
count = 0;
}
}
sb.Append("</div>");
lblStatus.Text = "Rendering PDF...";
Application.DoEvents();
RenderPdfAsync(sb.ToString());
}
private void RenderPdfAsync(string htmlString)
{
IronPdf.HtmlToPdf render = new IronPdf.HtmlToPdf();
lblStatus.Text = "Saved!";
render.RenderHtmlAsPdf(htmlString).SaveAs(folderPath + "/palette.pdf");
MessageBox.Show("File Saved to " + folderPath);
//pixelList = new List<Color>();
dict = new Dictionary<Color, int>();
btnDownload.Enabled = false;
btnSingleImage.Enabled = true;
btnFolder.Enabled = true;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnFolder_Click(object sender, EventArgs e)
{
using (var fbd = new FolderBrowserDialog())
{
DialogResult result = fbd.ShowDialog();
if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
{
var t = new Thread(() => pixelReadFolder(fbd));
t.Start();
lblStatus.Text = "Loading...";
}
}
}
public void pixelReadFolder(FolderBrowserDialog result)
{
folderPath = Path.GetFullPath(result.SelectedPath);
//pixelList = new List<Color>();
dict = new Dictionary<Color, int>();
int pixelCount = 0;
var allowedExtensions = new[] { ".jpg", ".jpeg", ".jpe", ".jfif", ".png", ".PNG" };
var files = Directory
.GetFiles(result.SelectedPath)
.Where(file => allowedExtensions.Any(file.ToLower().EndsWith))
.ToList();
progressBar1.Maximum = 0;
foreach (var file in files)
{
Bitmap img = new Bitmap(file);
progressBar1.Maximum += img.Width;
}
foreach (var file in files)
{
Bitmap img = new Bitmap(file);
pixelCount += img.Width + img.Height;
for (int i = 0; i < img.Width; i++)
{
for (int j = 0; j < img.Height; j++)
{
Color pixel = img.GetPixel(i, j);
if (!dict.Keys.Contains(pixel))
{
dict.Add(pixel, 1);
}
else
{
dict[pixel] += 1;
}
}
progressBar1.Value += 1;
}
}
progressBar1.Value = progressBar1.Maximum;
lblTotalPixelCount.Text = $"Pixel Count: {pixelCount}";
lblPixelCount.Text = $"Different Pixel Count: {dict.Count}";
lblStatus.Text = "Done";
btnDownload.Enabled = true;
btnSingleImage.Enabled = false;
btnFolder.Enabled = false;
}
private void btnRestart_Click(object sender, EventArgs e)
{
Application.Restart();
}
}
}