-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathProgram.cs
191 lines (174 loc) · 6.38 KB
/
Program.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
namespace ImgGen
{
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.IO;
using System.Threading;
internal class Program
{
private static PrivateFontCollection fontCollection;
private static ImageCodecInfo encoderInfo;
private static EncoderParameters encoderParams;
private static bool generateLarge;
private static bool generateSmall;
private static bool generateThumb;
private static Queue<string> files;
private static object locker = new object();
private static ImageCodecInfo GetEncoderInfo(string mimeType)
{
ImageCodecInfo[] imageEncoders = ImageCodecInfo.GetImageEncoders();
for (int i = 0; i < imageEncoders.Length; i++)
{
if (imageEncoders[i].MimeType == mimeType)
{
return imageEncoders[i];
}
}
return null;
}
public static FontFamily GetFontFamily(string fontName)
{
try
{
return new FontFamily(fontName, fontCollection);
}
catch
{
try
{
return new FontFamily(fontName);
}
catch
{
Console.WriteLine($"Font {fontName} not found!");
return new FontFamily("Arial");
}
}
}
private static void Main(string[] args)
{
fontCollection = new PrivateFontCollection();
if (Directory.Exists("./fonts"))
{
foreach (string font in Directory.GetFiles("./fonts"))
{
fontCollection.AddFontFile(font);
}
}
encoderInfo = GetEncoderInfo("image/jpeg");
encoderParams = new EncoderParameters(1);
EncoderParameter parameter = new EncoderParameter(Encoder.Quality, 90L);
encoderParams.Param[0] = parameter;
if (args.Length > 0)
{
DataManager.InitialDatas(args[0]);
}
else
{
DataManager.InitialDatas("../cards.cdb");
}
files = new Queue<string>();
foreach (string file in Directory.GetFiles("./pico", "*.png"))
{
files.Enqueue(file);
}
foreach (string file in Directory.GetFiles("./pico", "*.jpg"))
{
files.Enqueue(file);
}
generateLarge = System.Configuration.ConfigurationManager.AppSettings["GenerateLarge"] != "False"; // true if AppSettings null
generateSmall = System.Configuration.ConfigurationManager.AppSettings["GenerateSmall"] == "True";
generateThumb = System.Configuration.ConfigurationManager.AppSettings["GenerateThumb"] == "True";
if (generateLarge)
Directory.CreateDirectory("./picn");
if (generateSmall)
Directory.CreateDirectory("./pics");
if (generateThumb)
Directory.CreateDirectory("./pics/thumbnail");
for (int i = 0; i < Environment.ProcessorCount; i++)
{
Thread workThread = new Thread(Worker);
workThread.Start();
}
}
private static void Worker()
{
ImageManager imageManager;
lock (locker)
{
imageManager = new ImageManager();
imageManager.InitialDatas();
}
while (true)
{
string file;
lock (locker)
{
if (files.Count == 0)
return;
file = files.Dequeue();
}
Genernate(file, imageManager);
}
}
private static void Genernate(string srcName, ImageManager imageManager)
{
int code;
try
{
code = int.Parse(Path.GetFileNameWithoutExtension(srcName));
}
catch
{
return;
}
string fileName = code.ToString() + ".jpg";
Console.WriteLine($"Generating {fileName}");
Bitmap image = imageManager.GetImage(code);
if (image == null)
{
Console.WriteLine($"[{code}] generation failed");
return;
}
if (generateLarge)
{
image.Save("./picn/" + fileName, encoderInfo, encoderParams);
}
if (generateSmall)
{
Bitmap bmp = Zoom(image, 177, 254);
bmp.Save("./pics/" + fileName, encoderInfo, encoderParams);
bmp.Dispose();
}
if (generateThumb)
{
Bitmap bmp = Zoom(image, 44, 64);
bmp.Save("./pics/thumbnail/" + fileName, encoderInfo, encoderParams);
bmp.Dispose();
}
image?.Dispose();
}
public static Bitmap Zoom(Bitmap sourceBitmap, int newWidth, int newHeight)
{
if (sourceBitmap != null)
{
Bitmap b = new Bitmap(newWidth, newHeight);
Graphics graphics = Graphics.FromImage(b);
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
Rectangle newRect = new Rectangle(0, 0, newWidth, newHeight);
Rectangle srcRect = new Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height);
graphics.DrawImage(sourceBitmap, newRect, srcRect, GraphicsUnit.Pixel);
graphics.Dispose();
return b;
}
return null;
}
}
}