Search images recursively

This commit is contained in:
2017-07-06 01:24:58 +02:00
parent 01207d1a8f
commit c5f19146a6

View File

@@ -67,13 +67,37 @@ namespace ImageCrusher
return; return;
} }
String destPath = String.Format("{0}/small", path); String destPath;
if (path.EndsWith("/") || path.EndsWith("\\"))
{
path = path.Substring(0, path.Length - 1);
}
destPath = String.Format("{0}/small", path);
string currentPath = path;
int size = InitialSize; int size = InitialSize;
lstImagenes_Clean(); lstImagenes_Clean();
string[] fileNames = Directory.GetFiles(path); ProcessDirectory(currentPath, path, destPath, size);
lstImagenes_AddLine("End.");
}
private void ProcessDirectory(string currentPath, string path, string destPath, int size)
{
string[] fileNames = Directory.GetFiles(currentPath);
foreach (string fileName in fileNames) foreach (string fileName in fileNames)
{
ProcessFile(fileName, path, destPath, size);
}
string[] directories = Directory.GetDirectories(currentPath);
foreach (string directory in directories)
{
ProcessDirectory(directory, path, destPath, size);
}
}
private void ProcessFile(string fileName, string path, string destPath, int size)
{ {
string ext = Path.GetExtension(fileName).ToLower(); string ext = Path.GetExtension(fileName).ToLower();
if (ext == ".png" || ext == ".jpg" || ext == ".jpeg") if (ext == ".png" || ext == ".jpg" || ext == ".jpeg")
@@ -81,26 +105,15 @@ namespace ImageCrusher
try try
{ {
Image img = Image.FromFile(fileName); Image img = Image.FromFile(fileName);
int width = img.Width; Image imgThumb = Image_GenerateThumbnail(img, size);
int height = img.Height;
if (width > height)
{
height = (int)(height / (width / (double)size));
width = size;
}
else
{
width = (int)(width / (height / (double)size));
height = size;
}
if (!Directory.Exists(destPath)) string fileDirectory = Path.GetDirectoryName(fileName);
fileDirectory = fileDirectory.Replace(path, destPath);
if (!Directory.Exists(fileDirectory))
{ {
Directory.CreateDirectory(destPath); Directory.CreateDirectory(fileDirectory);
} }
string newFileName = String.Format("{0}/{1}.small.jpg", fileDirectory, Path.GetFileNameWithoutExtension(fileName));
Image imgThumb = Image_Resize(img, width, height);
string newFileName = String.Format("{0}/{1}.small.jpg", destPath, Path.GetFileNameWithoutExtension(fileName));
imgThumb.Save(newFileName, ImageFormat.Jpeg); imgThumb.Save(newFileName, ImageFormat.Jpeg);
// Hack: collect garbage now // Hack: collect garbage now
@@ -114,9 +127,24 @@ namespace ImageCrusher
} }
} }
} }
lstImagenes_AddLine("End.");
}
private Image Image_GenerateThumbnail(Image img, int size)
{
int width = img.Width;
int height = img.Height;
if (width > height)
{
height = (int)(height / (width / (double)size));
width = size;
}
else
{
width = (int)(width / (height / (double)size));
height = size;
}
Image imgThumb = Image_Resize(img, width, height);
return imgThumb;
}
public Image Image_Resize(Image img, int width, int height) public Image Image_Resize(Image img, int width, int height)
{ {