-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShrinkerClass.cs
executable file
·306 lines (272 loc) · 11.4 KB
/
ShrinkerClass.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
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Drawing.Imaging;
using System.Linq;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace FotoShrinker
{
public class ShrinkerClass
{
private List<string> FilesList = new List<string>();
private readonly int MaxTagLength = 900;
private void DirSearch(string sDir)
{
string searchMask = "*.jp*g";
try
{
foreach (string d in Directory.GetDirectories(sDir))
{
DirSearch(d);
}
FilesList.AddRange(Directory.GetFiles(sDir, searchMask));
}
catch (Exception excpt)
{
Console.WriteLine(excpt.Message);
}
}
public bool ShrinkFolder(string InFolder, string OutFolder, int maxWidth, int Quality, bool WriteTAG, IEnumerable<string> tagData, IEnumerable<string> tagResized, int ThreadsCount, bool RenameToNum)
{
if (Directory.Exists(InFolder))
{
if (!Directory.Exists(OutFolder))
Directory.CreateDirectory(OutFolder);
if (OutFolder.Last() != Path.DirectorySeparatorChar)
OutFolder += Path.DirectorySeparatorChar;
DirSearch(InFolder);
int fileNum = 1;
var tasks = new ConcurrentQueue<Task>();
foreach (var file in FilesList)
{
if (ThreadsCount > 1)
while (tasks.Count >= ThreadsCount)
{
Task.WaitAny(tasks.ToArray());
}
Task t = new Task(() =>
{
string filename = Path.GetFullPath(file).Replace(InFolder, OutFolder);
if (!IsTag(file))
{
Resize(file, InFolder, OutFolder, maxWidth, Quality, WriteTAG, tagData, tagResized, RenameToNum ? fileNum++ : -1);
}
else
{
if (InFolder.ToLower() != OutFolder.ToLower())
{
if (!File.Exists(filename))
File.Copy(file, filename, true);
}
}
tasks.TryDequeue(out t);
});
tasks.Enqueue(t);
t.Start();
}
}
return true;
}
public Snippets.Snippet Resize(string FileName, string InFolder, string OutFilePath, int MaxWidth, int Quality, bool WriteTAG, IEnumerable<string> tagData, IEnumerable<string> tagResized, int FileNum)
{
if (!Directory.Exists(OutFilePath))
Directory.CreateDirectory(OutFilePath);
long size = 0;
string filename = "";
if (FileNum == -1)
{
filename = OutFilePath + Path.GetFileName(FileName);
}
else
{
filename = OutFilePath + "Image#" + FileNum + Path.GetExtension(FileName);
}
string sdir = "";
foreach (var dir in Path.GetDirectoryName(filename).Split(Path.DirectorySeparatorChar))
{
sdir += dir + Path.DirectorySeparatorChar;
if (!Directory.Exists(sdir))
Directory.CreateDirectory(sdir);
}
DateTime fileDate = File.GetLastWriteTime(FileName).AddSeconds(1);
Bitmap image;
using (FileStream fsSource = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
size = fsSource.Length;
image = new Bitmap(fsSource);
fsSource.Close();
}
int originalWidth = image.Width;
int originalHeight = image.Height;
var snip = new Snippets.Snippet
{
Path = FileName,
Height = originalHeight,
Width = originalWidth,
Size = size,
Resized = false
};
if (Math.Max(originalWidth, originalHeight) > MaxWidth + 10)
{
float ratio = (float)MaxWidth / (float)Math.Max(originalWidth, originalHeight);
int newWidth = (int)(originalWidth * ratio);
int newHeight = (int)(originalHeight * ratio);
using (Bitmap newImage = new Bitmap(newWidth, newHeight, PixelFormat.Format24bppRgb))
{
using (Graphics graphics = Graphics.FromImage(newImage))
{
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.DrawImage(image, 0, 0, newWidth, newHeight);
}
ImageCodecInfo imageCodecInfo = ImageCodecInfo.GetImageDecoders().SingleOrDefault(c => c.FormatID == ImageFormat.Jpeg.Guid);
Encoder encoder = Encoder.Quality;
EncoderParameters encoderParameters = new EncoderParameters(1);
EncoderParameter encoderParameter = new EncoderParameter(encoder, Quality);
encoderParameters.Param[0] = encoderParameter;
try
{
if (File.Exists(filename))
File.Delete(filename);
using (FileStream fsImage = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.Write))
{
newImage.Save(fsImage, imageCodecInfo, encoderParameters);
}
}
finally
{
}
}
snip.Resized = true;
if (snip.Resized)
{
if (WriteTAG)
{
WriteTag(filename, tagData.ToArray().Concat(tagResized.ToArray()).Select(f => Snippets.FormatWith(f, new object[1] { snip })).ToArray());
}
}
else
{
if (WriteTAG)
WriteTag(filename, tagData);
}
File.SetCreationTime(filename, fileDate);
File.SetLastWriteTime(filename, fileDate);
}
else
{
if (InFolder.ToLower() != OutFilePath.ToLower())
{
if (!File.Exists(filename))
File.Copy(FileName, filename, true);
}
}
image.Dispose();
return snip;
}
public bool IsTag(string InFileName)
{
int buffLength = MaxTagLength;
byte[] tagData;
using (FileStream fs = new FileStream(InFileName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
fs.Seek(fs.Length - buffLength, SeekOrigin.Begin);
tagData = new byte[buffLength];
fs.Read(tagData, 0, buffLength);
fs.Close();
}
bool tagExists = false;
for (int i = buffLength - 1; i > 0; i--)
{
if (tagData[i] == 84)
{
if (tagData[i + 1] == 65)
{
if (tagData[i + 2] == 71)
{
if (tagData[i + 3] == 42)
{
tagExists = true;
break;
}
}
}
}
}
return tagExists;
}
public IEnumerable<string> ReadTag(string InFileName)
{
var tag = new List<string>();
int buffLength = MaxTagLength;
byte[] tagData;
using (FileStream fs = new FileStream(InFileName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
fs.Seek(fs.Length - buffLength, SeekOrigin.Begin);
tagData = new byte[buffLength];
fs.Read(tagData, 0, buffLength);
fs.Close();
}
int pos = -1;
for (int i = buffLength - 1; i > 0; i--)
{
if (tagData[i] == 84)
{
if (tagData[i + 1] == 65)
{
if (tagData[i + 2] == 71)
{
if (tagData[i + 3] == 42)
{
pos = i;
break;
}
}
}
}
}
if (pos != -1)
{
byte[] tagDataS = new byte[buffLength - pos];
for (int i = pos; i < buffLength; i++)
{
tagDataS[i - pos] = tagData[i];
}
string stag = string.Join("", tagDataS.Select(f => (char)f).Select(f => f.ToString()));
tag = stag.Split('*').Select(f => f.Replace("/&astrx", "*")).Skip(1).Where(f => !string.IsNullOrEmpty(f)).ToList();
}
return tag;
}
public void WriteTag(string InFileName, IEnumerable<string> Params)
{
if (File.Exists(InFileName))
{
var TAG = ReadTag(InFileName).ToArray();
if (!TAG.Any(f => Params.Any(x => x.ToLower() == f.ToLower())))
{
File.SetAttributes(InFileName, FileAttributes.Normal);
int buffLength = MaxTagLength;
DateTime fileDate = File.GetLastWriteTime(InFileName);
using (FileStream fs = new FileStream(InFileName, FileMode.Append, FileAccess.Write, FileShare.Write))
{
string tagS = "";
if (TAG.Count() == 0)
tagS = "TAG*";
tagS += string.Join("*", TAG.Concat(Params).Distinct().Select(f => f.Replace("*", "/&astrx"))) + "*";
byte[] tagData = tagS.ToArray().Select(f => (byte)f).ToArray();
if (tagData.Length < MaxTagLength + 1)
fs.Write(tagData, 0, tagData.Length);
fs.Close();
}
File.SetCreationTime(InFileName, fileDate.AddSeconds(1));
File.SetLastWriteTime(InFileName, fileDate.AddSeconds(1));
}
}
}
}
}