Commit c8eaf52d authored by mercury233's avatar mercury233

use multithread

parent edcd0708
This diff is collapsed.
This diff is collapsed.
...@@ -11,6 +11,21 @@ ...@@ -11,6 +11,21 @@
<TargetFrameworkProfile> <TargetFrameworkProfile>
</TargetFrameworkProfile> </TargetFrameworkProfile>
<FileAlignment>512</FileAlignment> <FileAlignment>512</FileAlignment>
<PublishUrl>publish\</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
<UpdateEnabled>false</UpdateEnabled>
<UpdateMode>Foreground</UpdateMode>
<UpdateInterval>7</UpdateInterval>
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
<UpdatePeriodically>false</UpdatePeriodically>
<UpdateRequired>false</UpdateRequired>
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<DebugSymbols>true</DebugSymbols> <DebugSymbols>true</DebugSymbols>
...@@ -49,6 +64,7 @@ ...@@ -49,6 +64,7 @@
<ItemGroup> <ItemGroup>
<Compile Include="Data.cs" /> <Compile Include="Data.cs" />
<Compile Include="DataManager.cs" /> <Compile Include="DataManager.cs" />
<Compile Include="ImageManager.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
...@@ -59,6 +75,13 @@ ...@@ -59,6 +75,13 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None> </None>
</ItemGroup> </ItemGroup>
<ItemGroup>
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
<Visible>False</Visible>
<ProductName>.NET Framework 3.5 SP1</ProductName>
<Install>true</Install>
</BootstrapperPackage>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSHARP.Targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSHARP.Targets" />
<ProjectExtensions> <ProjectExtensions>
<VisualStudio AllowExistingFolder="true" /> <VisualStudio AllowExistingFolder="true" />
......
...@@ -3,12 +3,25 @@ namespace ImgGen ...@@ -3,12 +3,25 @@ namespace ImgGen
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using System.Drawing.Text; using System.Drawing.Text;
using System.IO; using System.IO;
using System.Threading;
internal class Program 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) private static ImageCodecInfo GetEncoderInfo(string mimeType)
{ {
ImageCodecInfo[] imageEncoders = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo[] imageEncoders = ImageCodecInfo.GetImageEncoders();
...@@ -22,8 +35,6 @@ namespace ImgGen ...@@ -22,8 +35,6 @@ namespace ImgGen
return null; return null;
} }
private static PrivateFontCollection fontCollection;
public static FontFamily GetFontFamily(string fontName) public static FontFamily GetFontFamily(string fontName)
{ {
try try
...@@ -51,6 +62,12 @@ namespace ImgGen ...@@ -51,6 +62,12 @@ namespace ImgGen
{ {
fontCollection.AddFontFile(font); 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) if (args.Length > 0)
{ {
DataManager.InitialDatas(args[0]); DataManager.InitialDatas(args[0]);
...@@ -59,60 +76,112 @@ namespace ImgGen ...@@ -59,60 +76,112 @@ namespace ImgGen
{ {
DataManager.InitialDatas("../cards.cdb"); DataManager.InitialDatas("../cards.cdb");
} }
Encoder quality = Encoder.Quality;
ImageCodecInfo encoderInfo = GetEncoderInfo("image/jpeg"); files = new Queue<string>();
EncoderParameters encoderParams = new EncoderParameters(1); foreach (string file in Directory.GetFiles("./pico", "*.png"))
EncoderParameter parameter = new EncoderParameter(quality, 90L); {
encoderParams.Param[0] = parameter; files.Enqueue(file);
List<string> files = new List<string>(); }
files.AddRange(Directory.GetFiles("./pico", "*.png")); foreach (string file in Directory.GetFiles("./pico", "*.jpg"))
files.AddRange(Directory.GetFiles("./pico", "*.jpg")); {
bool generateLarge = System.Configuration.ConfigurationManager.AppSettings["GenerateLarge"] != "False"; // true if AppSettings null files.Enqueue(file);
bool generateSmall = System.Configuration.ConfigurationManager.AppSettings["GenerateSmall"] == "True"; }
bool generateThumb = System.Configuration.ConfigurationManager.AppSettings["GenerateThumb"] == "True";
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) if (generateLarge)
Directory.CreateDirectory("./picn"); Directory.CreateDirectory("./picn");
if (generateSmall) if (generateSmall)
Directory.CreateDirectory("./pics"); Directory.CreateDirectory("./pics");
if (generateThumb) if (generateThumb)
Directory.CreateDirectory("./pics/thumbnail"); Directory.CreateDirectory("./pics/thumbnail");
foreach (string str in files)
for (int i = 0; i < Environment.ProcessorCount; i++)
{ {
int code; Thread workThread = new Thread(Worker);
try workThread.Start();
{ }
code = int.Parse(Path.GetFileNameWithoutExtension(str)); }
}
catch private static void Worker()
{ {
continue; ImageManager imageManager;
} lock (locker)
string fileName = code.ToString() + ".jpg"; {
Console.WriteLine($"Generating {fileName}"); imageManager = new ImageManager();
Bitmap image = DataManager.GetImage(code); imageManager.InitialDatas();
if (image == null) }
{ while (true)
Console.WriteLine($"[{code}] generation failed"); {
continue; string file;
} lock (locker)
if (generateLarge)
{
image.Save("./picn/" + fileName, encoderInfo, encoderParams);
}
if (generateSmall)
{
Bitmap bmp = DataManager.Zoom(image, 177, 254);
bmp.Save("./pics/" + fileName, encoderInfo, encoderParams);
bmp.Dispose();
}
if (generateThumb)
{ {
Bitmap bmp = DataManager.Zoom(image, 44, 64); if (files.Count == 0)
bmp.Save("./pics/thumbnail/" + fileName, encoderInfo, encoderParams); return;
bmp.Dispose(); file = files.Dequeue();
} }
image?.Dispose(); 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;
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment