// 送られたパスの画像を圧縮する
private string ImageCoder(string path)
{
int fileCount = Directory.GetFiles(path).Count();
string[] fileTitles = Directory.GetFiles(path);
Array.Sort(fileTitles, new LogicalStringComparer());
string dirResize = path + @"\resize\";
Directory.CreateDirectory(dirResize);
ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);
System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
EncoderParameters myEncoderParameters = new EncoderParameters(1);
for (int i = 0; i < fileCount; i++)
{
System.Drawing.Bitmap bmpSrc = new System.Drawing.Bitmap(fileTitles[i]);
System.Drawing.Bitmap bmpSrcHalf = new System.Drawing.Bitmap
(bmpSrc, RE_WIDTH, (int)(bmpSrc.Height * ((double)RE_WIDTH) / (double)(bmpSrc.Width)));
EncoderParameter myEncoderParameter =
new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, RE_COMP);
myEncoderParameters.Param[0] = myEncoderParameter;
bmpSrcHalf.Save(dirResize + (i + 1) + ".jpg", jgpEncoder, myEncoderParameters);
FileInfo fileInfo = new FileInfo(dirResize + (i + 1) + ".jpg");
long reComp = RE_COMP;
if (fileInfo.Length < 3000)
{
ShowBalloon("画像は削除されています。処理を中断します。");
break;
}
while (fileInfo.Length > RE_SIZE)
{
myEncoderParameter = new EncoderParameter(myEncoder, (reComp = reComp - 3));
myEncoderParameters.Param[0] = myEncoderParameter;
bmpSrcHalf.Save(dirResize + (i + 1) + ".jpg", jgpEncoder, myEncoderParameters);
fileInfo = new FileInfo(dirResize + (i + 1) + ".jpg");
}
}
// ZIP化
ZipStream(dirResize, path);
return dirResize;
}
private ImageCodecInfo GetEncoder(ImageFormat format)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
foreach (ImageCodecInfo codec in codecs)
if (codec.FormatID == format.Guid)
return codec;
return null;
}
// 送られたパスのファイルをソートする
private string[] FileSort(string path)
{
string[] fileNames = Directory.GetFiles(path);
fileNames = fileNames.OrderBy(n =>
{
int v = 0;
if (int.TryParse(Path.GetFileNameWithoutExtension(n), out v))
{
return v;
}
return 9999999; // 数字で無いファイル名は一番後ろになる様に
}).ToArray();
return fileNames;
}